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

77 lines
3.5 KiB
PowerShell
Raw 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
2023-07-22 08:48:24 +00:00
#Get header titles [without remove seperator]
$index = $lines[$fl] -split '(?<=\s)(?!\s)'
2022-03-14 13:55:02 +00:00
2023-07-22 08:48:24 +00:00
# Line $fl has the header, we can find char where we find ID and Version [and manage non latin characters]
$idStart = [System.Text.Encoding]::UTF8.GetByteCount($($index[0] -replace '[\u4e00-\u9fa5]', '**'))
$versionStart = $idStart + [System.Text.Encoding]::UTF8.GetByteCount($($index[1] -replace '[\u4e00-\u9fa5]', '**'))
$availableStart = $versionStart + [System.Text.Encoding]::UTF8.GetByteCount($($index[2] -replace '[\u4e00-\u9fa5]', '**'))
2022-03-14 13:55:02 +00:00
# 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++) {
2023-07-28 09:47:27 +00:00
$line = $lines[$i] -replace "[\u2026]", " " #Fix "..." in long names
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-07-22 08:48:24 +00:00
#Get header titles [without remove seperator]
$index = $lines[$fl] -split '(?<=\s)(?!\s)'
2023-03-17 19:57:08 +00:00
2023-07-22 08:48:24 +00:00
# Line $fl has the header, we can find char where we find ID and Version [and manage non latin characters]
$idStart = [System.Text.Encoding]::UTF8.GetByteCount($($index[0] -replace '[\u4e00-\u9fa5]', '**'))
$versionStart = $idStart + [System.Text.Encoding]::UTF8.GetByteCount($($index[1] -replace '[\u4e00-\u9fa5]', '**'))
$availableStart = $versionStart + [System.Text.Encoding]::UTF8.GetByteCount($($index[2] -replace '[\u4e00-\u9fa5]', '**'))
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()
#Manage non latin characters
$nameDeclination = $([System.Text.Encoding]::UTF8.GetByteCount($($line.Substring(0, $idStart) -replace '[\u4e00-\u9fa5]', '**')) - $line.Substring(0, $idStart).Length)
$software.Name = $line.Substring(0, $idStart - $nameDeclination).TrimEnd()
$software.Id = $line.Substring($idStart - $nameDeclination, $versionStart - $idStart).TrimEnd()
$software.Version = $line.Substring($versionStart - $nameDeclination, $availableStart - $versionStart).TrimEnd()
$software.AvailableVersion = $line.Substring($availableStart - $nameDeclination).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
}