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 " ----- " ) ) {
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-15 23:04:24 +00:00
$lines = $upgradeResult . Split ( [ Environment ] :: NewLine ) | Where-Object { $_ -and ( $_ -notmatch " --include-unknown " ) -and ( $_ -notmatch " update the source: " ) -and ( $_ -notmatch " require explicit targeting " ) -and ( $_ -notmatch " ^(\d*)\s+(upgrade\w*)\s+(available\.) $ " ) }
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-15 23:04:24 +00:00
if ( $line -and $line -match " ^(Name)\s+(Id)\s+(Version)\s+(Available) $ " ) {
#Get header titles
$index = $lines [ $fl ] -split '\s+'
$idStart = $lines [ $i ] . IndexOf ( $index [ 1 ] )
$versionStart = $lines [ $i ] . IndexOf ( $index [ 2 ] )
$availableStart = $lines [ $i ] . IndexOf ( $index [ 3 ] )
}
if ( $line -and ( $line -notmatch " ^(Name)\s+(Id)\s+(Version)\s+(Available) $ " ) -and ( -not $line . StartsWith ( " ----- " ) ) ) {
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
}
}
2022-10-08 00:18:46 +00:00
#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-08 00:18:46 +00:00
}
2022-10-18 13:23:39 +00:00
return $upgradeList | Sort-Object { Get-Random }
2022-10-26 22:49:10 +00:00
}