Skip to content

Instantly share code, notes, and snippets.

@alkampfergit
Last active December 8, 2024 12:57
Show Gist options
  • Save alkampfergit/19f89c1a93cc1e7b9ec9bf501f2b9134 to your computer and use it in GitHub Desktop.
Save alkampfergit/19f89c1a93cc1e7b9ec9bf501f2b9134 to your computer and use it in GitHub Desktop.
Winget upgrade output parsed into a real Powershell Object
class Software {
[string]$Name
[string]$Id
[string]$Version
[string]$AvailableVersion
}
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$upgradeResult = winget upgrade | Out-String
$lines = $upgradeResult.Split([Environment]::NewLine)
# Find the line that starts with Name, it contains the header
$fl = 0
while (-not $lines[$fl].StartsWith("Name"))
{
$fl++
}
# Line $i has the header, we can find char where we find ID and Version
$idStart = $lines[$fl].IndexOf("Id")
$versionStart = $lines[$fl].IndexOf("Version")
$availableStart = $lines[$fl].IndexOf("Available")
$sourceStart = $lines[$fl].IndexOf("Source")
# Now cycle in real package and split accordingly
$upgradeList = @()
For ($i = $fl + 1; $i -le $lines.Length; $i++)
{
$line = $lines[$i]
if ($line.Length -gt ($availableStart + 1) -and -not $line.StartsWith('-'))
{
$name = $line.Substring(0, $idStart).TrimEnd()
$id = $line.Substring($idStart, $versionStart - $idStart).TrimEnd()
$version = $line.Substring($versionStart, $availableStart - $versionStart).TrimEnd()
$available = $line.Substring($availableStart, $sourceStart - $availableStart).TrimEnd()
$software = [Software]::new()
$software.Name = $name;
$software.Id = $id;
$software.Version = $version
$software.AvailableVersion = $available;
$upgradeList += $software
}
}
$upgradeList | Get-Member
$upgradeList | Format-Table
@JoeyNice
Copy link

JoeyNice commented Dec 8, 2024

In addition to the language setting, it also makes a difference whether you use Powershell, Powershell ISE or Visual Studio Code.

This has a particular effect on the truncation of text. Sometimes it is a character string like  and sometimes it is a special character that looks like 3 dots (Unicode ellipsis [char]0x2026).

I encountered this problem in a similar project.

https://gist.github.com/nouseforname/a8b7ebcb9d0c05e380c7e3c81c300923?permalink_comment_id=5232687#gistcomment-5232687

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment