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

76 lines
2.6 KiB
PowerShell
Raw Normal View History

2022-03-22 13:39:01 +00:00
function Get-WingetOutdatedApps {
2022-03-14 13:55:02 +00:00
class Software {
[string]$Name
[string]$Id
[string]$Version
[string]$AvailableVersion
}
2022-04-04 13:54:16 +00:00
#Get WinGet Path
$WingetPath = (Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe").Path
#Get Winget Location in User context
2022-03-14 13:55:02 +00:00
if ($WingetCmd){
2022-04-04 13:54:16 +00:00
$Script:winget = (Get-Command winget.exe -ErrorAction SilentlyContinue).Source
2022-03-14 13:55:02 +00:00
}
2022-04-04 13:54:16 +00:00
#Get Winget Location in System context (WinGet < 1.17)
elseif (Test-Path "$WingetPath\AppInstallerCLI.exe"){
$Script:winget = "$WingetPath\AppInstallerCLI.exe"
2022-03-14 13:55:02 +00:00
}
2022-04-04 13:54:16 +00:00
#Get Winget Location in System context (WinGet > 1.17)
elseif (Test-Path "$WingetPath\winget.exe"){
$Script:winget = "$WingetPath\winget.exe"
2022-03-14 13:55:02 +00:00
}
else{
2022-04-04 13:54:16 +00:00
Write-Log "Winget not installed !" "Red"
break
2022-03-14 13:55:02 +00:00
}
#Run winget to list apps and accept source agrements (necessary on first run)
& $upgradecmd list --accept-source-agreements | Out-Null
#Get list of available upgrades on winget format
$upgradeResult = & $upgradecmd upgrade | Out-String
#Start Convertion of winget format to an array. Check if "-----" exists
if (!($upgradeResult -match "-----")){
return
}
#Split winget output to lines
$lines = $upgradeResult.Split([Environment]::NewLine).Replace("¦ ","")
# Find the line that starts with "------"
$fl = 0
while (-not $lines[$fl].StartsWith("-----")){
$fl++
}
#Get header line
$fl = $fl - 2
#Get header titles
$index = $lines[$fl] -split '\s+'
# Line $i has the header, we can find char where we find ID and Version
$idStart = $lines[$fl].IndexOf($index[1])
$versionStart = $lines[$fl].IndexOf($index[2])
$availableStart = $lines[$fl].IndexOf($index[3])
$sourceStart = $lines[$fl].IndexOf($index[4])
# Now cycle in real package and split accordingly
$upgradeList = @()
For ($i = $fl + 2; $i -le $lines.Length; $i++){
$line = $lines[$i]
if ($line.Length -gt ($sourceStart+5) -and -not $line.StartsWith('-')){
$software = [Software]::new()
$software.Name = $line.Substring(0, $idStart).TrimEnd()
$software.Id = $line.Substring($idStart, $versionStart - $idStart).TrimEnd()
$software.Version = $line.Substring($versionStart, $availableStart - $versionStart).TrimEnd()
$software.AvailableVersion = $line.Substring($availableStart, $sourceStart - $availableStart).TrimEnd()
#add formated soft to list
$upgradeList += $software
}
}
return $upgradeList
}