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

55 lines
1.9 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
}
#Get list of available upgrades on winget format
2022-04-08 16:37:59 +00:00
Write-Log "Checking application updates on Winget Repository..." "yellow"
2022-04-05 13:17:18 +00:00
$upgradeResult = & $Winget upgrade | Out-String
2022-03-14 13:55:02 +00:00
#Start Convertion of winget format to an array. Check if "-----" exists
if (!($upgradeResult -match "-----")){
return
}
#Split winget output to lines
2022-04-08 16:30:58 +00:00
$lines = $upgradeResult.Split([Environment]::NewLine) | Where-Object {$_}
2022-03-14 13:55:02 +00:00
# Find the line that starts with "------"
$fl = 0
while (-not $lines[$fl].StartsWith("-----")){
$fl++
}
2022-04-07 22:50:28 +00:00
#Get header line
$fl = $fl - 1
2022-03-14 13:55:02 +00:00
#Get header titles
$index = $lines[$fl] -split '\s+'
2022-04-08 16:30:58 +00:00
# Line $fl has the header, we can find char where we find ID and Version
2022-03-14 13:55:02 +00:00
$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]
2022-04-08 16:30:58 +00:00
if ($line.Length -gt ($sourceStart+5) -and -not $line.Contains("--include-unknown")){
2022-03-14 13:55:02 +00:00
$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
}