Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-WinGetPackageUpdate if results contains packages that require explicit targeting for upgrade #22

Closed
o-l-a-v opened this issue Feb 21, 2023 · 0 comments

Comments

@o-l-a-v
Copy link

o-l-a-v commented Feb 21, 2023

This is a special case that broke my parsing logic for converting WinGet text output to PowerShell object too. I think it's related to the following:

Output of regular winget upgrade in CMD:

Click to expand
C:\Users\olav.birkeland>winget upgrade
Name                                      Id                        Version          Available        Source
------------------------------------------------------------------------------------------------------------
Signal 6.4.1                              OpenWhisperSystems.Signal 6.4.1            6.5.1            winget
Git                                       Git.Git                   2.39.1           2.39.2           winget
Kate                                      KDE.Kate                  22.12.1          22.12.2          winget
KeePass Password Safe 2.53                DominikReichl.KeePass     2.53             2.53.1           winget
Microsoft 365 Apps for enterprise - en-us Microsoft.Office          16.0.16026.20146 16.0.16026.20200 winget
Microsoft 365 Apps for enterprise - nb-no Microsoft.Office          16.0.16026.20146 16.0.16026.20200 winget
Plex                                      Plex.Plex                 1.62.2           1.64.2           winget
Microsoft Teams                           Microsoft.Teams           1.5.00.36367     1.6.00.376       winget
Authy Desktop                             Twilio.Authy              2.2.2            2.2.3            winget
Teams Machine-Wide Installer              Microsoft.Teams           1.5.0.8070       1.6.00.376       winget
11 upgrades available.

The following packages have an upgrade available, but require explicit targeting for upgrade:
Name    Id              Version  Available Source
-------------------------------------------------
Discord Discord.Discord 1.0.9010 1.0.9011  winget

C:\Users\olav.birkeland>

Output from Cobalt\Get-WinGetPackageUpdate

Click to expand
PS C:\WINDOWS\system32> Cobalt\Get-WinGetPackageUpdate

ForEach-Object : Exception calling "Substring" with "2" argument(s): "startIndex cannot be larger than length of string.
Parameter name: startIndex"
At C:\Users\olav.birkeland\Desktop\cobalt.0.4.0\Cobalt.psm1:1802 char:191
+ ... atin}]+',' ' | Select-Object -Skip ($headerLine+2) | ForEach-Object {
+                                                          ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [ForEach-Object], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.ForEachObjectCommand
 

PS C:\WINDOWS\system32>

Winget info:

Click to expand
Windows Package Manager v1.4.10173
Copyright (c) Microsoft Corporation. All rights reserved.

Windows: Windows.Desktop v10.0.22621.1265
System Architecture: X64
Package: Microsoft.DesktopAppInstaller v1.19.10173.0

PowerShell info (running ISE as admin):

Click to expand
PS C:\WINDOWS\system32> $PSVersionTable

Name                           Value                                                                                                                      
----                           -----                                                                                                                      
PSVersion                      5.1.22621.963                                                                                                              
PSEdition                      Desktop                                                                                                                    
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                    
BuildVersion                   10.0.22621.963                                                                                                             
CLRVersion                     4.0.30319.42000                                                                                                            
WSManStackVersion              3.0                                                                                                                        
PSRemotingProtocolVersion      2.3                                                                                                                        
SerializationVersion           1.1.0.1                                                                                                                    



PS C:\WINDOWS\system32> Get-Module -Name 'Cobalt'

ModuleType Version    Name                                ExportedCommands                                                                                
---------- -------    ----                                ----------------                                                                                
Script     0.4.0      Cobalt                              {Find-WinGetPackage, Get-WinGetPackage, Get-WinGetPackageInfo, Get-WinGetPackageUpdate...}      



PS C:\WINDOWS\system32>

My jank workaround (probably not applicable here?):

Click to expand
## Get output from "winget upgrade"
### Get
do {
    $WingetUpgradeableRaw = [string[]](
        cmd /c ('{0} upgrade --source winget' -f $WingetCliPath) | Where-Object -FilterScript {$_[0] -notmatch '\s'}
    )
}
while (
    $(
        if ($WingetUpgradeableRaw.'Count' -eq 1 -and $WingetUpgradeableRaw[0] -like 'No installed*') {
            $false
        }
        else {
            $WingetUpgradeableRaw[1][0] -ne '-'
        }
    )
)

### Exit if no packages to update
if ($WingetUpgradeableRaw.'Count' -eq 1) {
    Write-Information -MessageData ('No packages to update: "{0}".' -f $WingetUpgradeableRaw[0])
    Exit 1
}

### Clean up
#### Remove empty entries
$WingetUpgradeable = [string[]]($WingetUpgradeableRaw.Where{-not[string]::IsNullOrEmpty($_)})
#### Remove "x upgrades available"
$WingetUpgradeable = [string[]]($WingetUpgradeable.Where{-not $_.EndsWith('upgrades available.')})
#### Remove "require explicit targeting" packages
$WingetUpgradeable = [string[]](
    $(
        foreach ($Line in $WingetUpgradeable) {
            if ($Line -like '*require explicit targeting*') {
                break
            }
            else {
                $Line
            }
        }
    )
)

### Sort output alphabetically
$WingetUpgradeable = [string[]](
    $WingetUpgradeable[0,1] + ($WingetUpgradeable[2 .. $WingetUpgradeable.IndexOf($WingetUpgradeable[-2])] | Sort-Object) + $WingetUpgradeable[-1]
)
@ethanbergstrom ethanbergstrom closed this as not planned Won't fix, can't repro, duplicate, stale Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants