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

62 lines
2.1 KiB
PowerShell
Raw Normal View History

2022-04-13 16:50:06 +00:00
#Function to get outdated app list, in formatted array
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-07-06 15:43:51 +00:00
$upgradeResult = & $Winget upgrade --source winget | Out-String
2022-03-14 13:55:02 +00:00
2022-12-11 00:31:44 +00:00
#Start Convertion of winget format to an array. Check if "-----" exists (Winget Error Handling)
2022-06-10 08:26:41 +00:00
if (!($upgradeResult -match "-----")) {
2022-12-11 11:02:36 +00:00
return "Problem:`n$upgradeResult"
2022-03-14 13:55:02 +00:00
}
#Split winget output to lines
$lines = $upgradeResult.Split([Environment]::NewLine) | Where-Object { $_ -and $_ -notmatch "--include-unknown" }
2022-03-14 13:55:02 +00:00
# Find the line that starts with "------"
$fl = 0
2022-06-10 08:26:41 +00:00
while (-not $lines[$fl].StartsWith("-----")) {
2022-03-14 13:55:02 +00:00
$fl++
}
2022-10-26 22:49:10 +00:00
#Get header line
2022-04-07 22:50:28 +00:00
$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])
# Now cycle in real package and split accordingly
$upgradeList = @()
2022-10-18 13:23:39 +00:00
For ($i = $fl + 2; $i -lt $lines.Length - 1; $i++) {
2022-03-14 13:55:02 +00:00
$line = $lines[$i]
2022-07-06 15:43:51 +00:00
if ($line) {
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()
2022-07-06 15:43:51 +00:00
$software.AvailableVersion = $line.Substring($availableStart).TrimEnd()
2022-03-14 13:55:02 +00:00
#add formated soft to list
$upgradeList += $software
}
}
#If current user is not system, remove system apps from list
if ($IsSystem -eq $false) {
$SystemApps = Get-Content -Path "$WorkingDir\winget_system_apps.txt"
2022-10-18 13:23:39 +00:00
$upgradeList = $upgradeList | Where-Object { $SystemApps -notcontains $_.Id }
}
2022-10-18 13:23:39 +00:00
return $upgradeList | Sort-Object { Get-Random }
2022-10-26 22:49:10 +00:00
}