Skip to content

Commit

Permalink
fix: Improved detection by discovering winget location
Browse files Browse the repository at this point in the history
Thanks Rudy Ooms
Fixed: #23
  • Loading branch information
svrooij committed Dec 27, 2023
1 parent 944d439 commit 2d19e4a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
40 changes: 37 additions & 3 deletions scripts/detection-with-version.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
$packageId = "7zip.7zip"
$version = "23.02.0.0"
$packageId = "Sonos.Controller"
$version = "73.0.41050"

$wingetOutput = & "winget" "list" "--id" $packageId "--exact" "--disable-interactivity" "--accept-source-agreements"
$transcriptionPath = "$env:ProgramData\Microsoft\IntuneManagementExtension\Logs\$packageId-detection.log"
Start-Transcript -Path $transcriptionPath -Force

Write-Host "Starting $packageId detection, log location: $transcriptionPath"

# Need to get the full path of winget, because detection script is run in a different context
Function Get-WingetCmd {

$WingetCmd = $null
#Get WinGet Path

try {
# Get Admin Context Winget Location
$WingetInfo = (Get-Item "$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller_*_8wekyb3d8bbwe\winget.exe").VersionInfo | Sort-Object -Property FileVersionRaw
# if multiple versions, pick most recent one
$WingetCmd = $WingetInfo[-1].FileName
}
catch {
#Get User context Winget Location
if (Test-Path "$env:LocalAppData\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe")
{
$WingetCmd ="$env:LocalAppData\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe"
}
}
Write-Host "Winget location: $WingetCmd"
return $WingetCmd
}

$wingetCmd = Get-WingetCmd
if ($null -eq $wingetCmd) {
Write-Host "winget not detected"
Exit 1
}

$wingetOutput = & $wingetCmd "list" "--id" $packageId "--exact" "--disable-interactivity" "--accept-source-agreements"

if($wingetOutput -is [array]) {
$lastRow = $wingetOutput[$wingetOutput.Length -1]
Expand Down
35 changes: 34 additions & 1 deletion src/WingetIntune/Intune/IntuneManagerConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,40 @@ Exit 5
internal const string PsDetectionCommandTemplate = @"$packageId = ""{packageId}""
$version = ""{version}""
$wingetOutput = & ""winget"" ""list"" ""--id"" $packageId ""--exact"" ""--disable-interactivity"" ""--accept-source-agreements""
$transcriptionPath = ""$env:ProgramData\Microsoft\IntuneManagementExtension\Logs\$packageId-detection.log""
Start-Transcript -Path $transcriptionPath -Force
Write-Host ""Starting $packageId detection, log location: $transcriptionPath""
# Need to get the full path of winget, because detection script is run in a different context
Function Get-WingetCmd {
$WingetCmd = $null
#Get WinGet Path
try {
# Get Admin Context Winget Location
$WingetInfo = (Get-Item ""$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller_*_8wekyb3d8bbwe\winget.exe"").VersionInfo | Sort-Object -Property FileVersionRaw
# if multiple versions, pick most recent one
$WingetCmd = $WingetInfo[-1].FileName
}
catch {
#Get User context Winget Location
if (Test-Path ""$env:LocalAppData\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe"")
{
$WingetCmd =""$env:LocalAppData\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe""
}
}
Write-Host ""Winget location: $WingetCmd""
return $WingetCmd
}
$wingetCmd = Get-WingetCmd
if ($null -eq $wingetCmd) {
Write-Host ""winget not detected""
Exit 1
}
$wingetOutput = & $wingetCmd ""list"" ""--id"" $packageId ""--exact"" ""--disable-interactivity"" ""--accept-source-agreements""
if($wingetOutput -is [array]) {
$lastRow = $wingetOutput[$wingetOutput.Length -1]
Expand Down
1 change: 1 addition & 0 deletions src/WingetIntune/Models/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public Win32LobApp ToWin32LobApp(PackageInfo packageInfo)
new Win32LobAppPowerShellScriptDetection
{
ScriptContent = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(packageInfo.DetectionScript!)),
EnforceSignatureCheck = false,
}
};
}
Expand Down

0 comments on commit 2d19e4a

Please sign in to comment.