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

93 lines
3.3 KiB
PowerShell
Raw Normal View History

# Function to get the outdated app list, in formatted array
2023-09-15 14:38:54 +00:00
function Get-WingetOutdatedApps
{
class Software {
[string]$Name
[string]$Id
[string]$Version
[string]$AvailableVersion
}
2023-09-15 14:38:54 +00:00
# Get list of available upgrades on winget format
$upgradeResult = (& $Winget upgrade --source winget | Out-String)
2023-09-15 14:38:54 +00:00
# Start Convertion of winget format to an array. Check if "-----" exists (Winget Error Handling)
2023-09-15 14:38:54 +00:00
if (!($upgradeResult -match '-----'))
{
return "An unusual thing happened (maybe all apps are upgraded):`n$upgradeResult"
}
2023-09-15 14:38:54 +00:00
# Split winget output to lines
$lines = $upgradeResult.Split([Environment]::NewLine) | Where-Object -FilterScript {
2023-09-15 14:38:54 +00:00
$_
}
2023-09-15 14:38:54 +00:00
# Find the line that starts with "------"
$fl = 0
2023-09-15 14:38:54 +00:00
while (-not $lines[$fl].StartsWith('-----'))
{
$fl++
}
2023-09-15 14:38:54 +00:00
# Get header line
$fl = $fl - 1
2023-09-15 14:38:54 +00:00
# Get header titles [without remove seperator]
$index = $lines[$fl] -split '(?<=\s)(?!\s)'
2023-09-15 14:38:54 +00:00
# Line $fl has the header, we can find char where we find ID and Version [and manage non latin characters]
$idStart = $($index[0] -replace '[\u4e00-\u9fa5]', '**').Length
$versionStart = $idStart + $($index[1] -replace '[\u4e00-\u9fa5]', '**').Length
$availableStart = $versionStart + $($index[2] -replace '[\u4e00-\u9fa5]', '**').Length
2023-09-15 14:38:54 +00:00
# Now cycle in real package and split accordingly
$upgradeList = @()
2023-09-15 14:38:54 +00:00
for ($i = $fl + 2; $i -lt $lines.Length; $i++)
{
$line = $lines[$i] -replace '[\u2026]', ' ' #Fix "..." in long names
2023-09-15 14:38:54 +00:00
if ($line.StartsWith('-----'))
{
# Get header line
$fl = $i - 1
2023-09-15 14:38:54 +00:00
# Get header titles [without remove seperator]
$index = $lines[$fl] -split '(?<=\s)(?!\s)'
2023-09-15 14:38:54 +00:00
# Line $fl has the header, we can find char where we find ID and Version [and manage non latin characters]
$idStart = $($index[0] -replace '[\u4e00-\u9fa5]', '**').Length
$versionStart = $idStart + $($index[1] -replace '[\u4e00-\u9fa5]', '**').Length
$availableStart = $versionStart + $($index[2] -replace '[\u4e00-\u9fa5]', '**').Length
}
2023-09-15 14:38:54 +00:00
# (Alphanumeric | Literal . | Alphanumeric) - the only unique thing in common for lines with applications
2023-09-15 14:38:54 +00:00
if ($line -match '\w\.\w')
{
$software = [Software]::new()
# Manage non latin characters
$nameDeclination = $($line.Substring(0, $idStart) -replace '[\u4e00-\u9fa5]', '**').Length - $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()
# add formated soft to list
$upgradeList += $software
}
}
2023-09-15 14:38:54 +00:00
# If current user is not system, remove system apps from list
2023-09-15 14:38:54 +00:00
if ($IsSystem -eq $false)
{
$SystemApps = Get-Content -Path ('{0}\winget_system_apps.txt' -f $WorkingDir)
$upgradeList = $upgradeList | Where-Object -FilterScript {
2023-09-15 14:38:54 +00:00
$SystemApps -notcontains $_.Id
}
}
2023-09-15 14:38:54 +00:00
return $upgradeList | Sort-Object -Property {
2023-09-15 14:38:54 +00:00
Get-Random
}
2022-10-26 22:49:10 +00:00
}