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

54 lines
1.5 KiB
PowerShell
Raw Normal View History

# Function to get the winget command regarding execution context (User, System...)
2022-04-13 16:50:06 +00:00
2023-09-15 14:38:54 +00:00
function Get-WingetCmd
{
2023-09-15 14:38:54 +00:00
# Get WinGet Path (if Admin context)
# Includes Workaround for ARM64 (removed X64 and replaces it with a wildcard)
$ResolveWingetPath = (Resolve-Path -Path "$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe" | Sort-Object -Property {
[version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1')
})
2023-09-15 14:38:54 +00:00
if ($ResolveWingetPath)
{
# If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
}
2023-09-15 14:38:54 +00:00
#If running under System or Admin context obtain Winget from Program Files
if ((([Security.Principal.WindowsIdentity]::GetCurrent().User) -eq 'S-1-5-18') -or ($WingetPath))
{
if (Test-Path -Path ('{0}\winget.exe' -f $WingetPath) -ErrorAction SilentlyContinue)
{
$Script:Winget = ('{0}\winget.exe' -f $WingetPath)
}
}
else
{
#Get Winget Location in User context
$WingetCmd = (Get-Command -Name winget.exe -ErrorAction SilentlyContinue)
2023-09-15 14:38:54 +00:00
if ($WingetCmd)
{
$Script:Winget = $WingetCmd.Source
}
}
2023-09-15 14:38:54 +00:00
if (!($Script:Winget))
{
Write-ToLog 'Winget not installed or detected !' 'Red'
2023-09-15 14:38:54 +00:00
return $false
}
2023-09-15 14:38:54 +00:00
# Run winget to list apps and accept source agrements (necessary on first run)
$null = (& $Winget list --accept-source-agreements -s winget)
2023-09-15 14:38:54 +00:00
# Log Winget installed version
$WingetVer = & $Winget --version
Write-ToLog ('Winget Version: {0}' -f $WingetVer)
2023-09-15 14:38:54 +00:00
return $true
2022-04-15 08:07:04 +00:00
}