wingetautoupdate/Winget-AutoUpdate/functions/Get-WingetCmd.ps1

38 lines
1.3 KiB
PowerShell
Raw Permalink Normal View History

2023-03-31 15:56:07 +00:00
#Function to get the winget command regarding execution context (User, System...)
2022-04-13 16:50:06 +00:00
2022-04-13 08:50:24 +00:00
Function Get-WingetCmd {
2022-04-13 16:50:06 +00:00
2022-04-13 08:50:24 +00:00
#Get WinGet Path (if admin context)
2022-10-25 15:18:58 +00:00
# Includes Workaround for ARM64 (removed X64 and replaces it with a wildcard)
$ResolveWingetPath = Resolve-Path "$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
2022-06-10 08:26:41 +00:00
if ($ResolveWingetPath) {
2022-04-13 08:50:24 +00:00
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
}
#Get Winget Location in User context
$WingetCmd = Get-Command winget.exe -ErrorAction SilentlyContinue
2022-06-10 08:26:41 +00:00
if ($WingetCmd) {
2022-04-13 08:50:24 +00:00
$Script:Winget = $WingetCmd.Source
}
#Get Winget Location in System context
2022-06-10 08:26:41 +00:00
elseif (Test-Path "$WingetPath\winget.exe") {
2022-04-13 08:50:24 +00:00
$Script:Winget = "$WingetPath\winget.exe"
}
2022-06-10 08:26:41 +00:00
else {
Write-ToLog "Winget not installed or detected !" "Red"
2022-04-13 08:50:24 +00:00
return $false
}
#Run winget to list apps and accept source agrements (necessary on first run)
& $Winget list --accept-source-agreements -s winget | Out-Null
2022-04-13 08:50:24 +00:00
#Log Winget installed version
2022-04-15 08:07:04 +00:00
$WingetVer = & $Winget --version
Write-ToLog "Winget Version: $WingetVer"
2022-10-25 15:18:58 +00:00
2022-04-13 08:50:24 +00:00
return $true
2022-04-13 16:50:06 +00:00
2022-04-15 08:07:04 +00:00
}