How can I log Winget?

74 Views Asked by At

I have been trying to write a script that will first run the

winget upgrade --all --accept-package-agreements --accept-source-agreements --silent

command and then log the results of it. Meaning that there will be packages that will fail to install due to hash mismatch - this is OK for me - and there will be ones that will be installed without issues. I want to parse these 2 types of installs: 1) Successful and 2) Failed into 2 tables (Format-Table if I understand correctly) inside a .log file in a folder set up by me.

The part I am having major issues is that I cannot fetch the return codes (ie, did the package install or did it fail to?) that winget throw, and based on that I cannot parse the output into those 2 tables consisting of: AppID, Name (of software), Size (of package), Date (of install or failure to install)

Can someone please help me?

Tried looking for this in the official winget documentation and couldn't find anything. They do mention --verbose, --verbonse-logs and --open-logs but they don't do what I'm trying to achieve here.

1

There are 1 best solutions below

0
Trenly On

If you're using the winget CLI you will need to parse the text that the executable puts into the command line output. However, if you use the Microsoft.WinGet.Client PowerShell Module, you can store the result of Install-WinGetPackage into a variable and access the Status and InstallerErrorCode properties or access them directly through the Pipeline

PS C:\Users\Trenly> Install-WinGetPackage Google.Chrome | Select-Object -Property Name,Status,InstallerErrorCode

Name          Status       InstallerErrorCode
----          ------       ------------------
Google Chrome InstallError                  3

To do this in the upgrade flow, you would need to use Get-WinGetPackage to get the list of packages that are on the machine, filter by the property IsUpdateAvailable, pass that through to Update-WinGetPackage, and then get the desired properties from the output.

This command should do the trick -

Get-WinGetPackage | Where-Object {$_.IsUpdateAvailable} | Update-WinGetPackage | Select-Object -Property Name,Status,InstallerErrorCode

Example output -

PS C:\Users\Trenly> Get-winGetPackage | Where-Object {$_.IsUpdateAvailable} | Update-WinGetPackage | Select-Object -Property Name,Status,InstallerErrorCode

Name                   Status       InstallerErrorCode
----                   ------       ------------------
GitHub Desktop         Ok                            0
Corsair iCUE5 Software InstallError         3221226525
Node.js                Ok                            0