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

75 lines
2.7 KiB
PowerShell
Raw Permalink Normal View History

2023-03-31 15:56:07 +00:00
#Function to get the outdated app list, in formatted array
2022-04-13 16:50:06 +00:00
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 "-----")) {
2023-02-04 00:08:38 +00:00
return "An unusual thing happened (maybe all apps are upgraded):`n$upgradeResult"
2022-03-14 13:55:02 +00:00
}
#Split winget output to lines
2023-03-17 19:57:08 +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
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 = @()
2023-03-15 23:04:24 +00:00
For ($i = $fl + 2; $i -lt $lines.Length; $i++) {
2022-03-14 13:55:02 +00:00
$line = $lines[$i]
2023-03-19 09:44:52 +00:00
if ($line.StartsWith("-----")) {
2023-03-17 19:57:08 +00:00
#Get header line
$fl = $i - 1
2023-03-15 23:04:24 +00:00
#Get header titles
2023-03-17 19:57:08 +00:00
$index = $lines[$fl] -split '\s+'
# Line $fl 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])
2023-03-31 15:56:07 +00:00
}
#(Alphanumeric | Literal . | Alphanumeric) - the only unique thing in common for lines with applications
2023-03-19 09:44:52 +00:00
if ($line -match "\w\.\w") {
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
}