Created
February 7, 2020 03:15
-
-
Save bender-the-greatest/7b117053f0acd4b9d929ce623cdb7bf1 to your computer and use it in GitHub Desktop.
Get outdated Chocolatey packages on the local system as a usable PowerShell object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-ChocolateyOutdatedPackages { | |
<# | |
.SYNOPSIS | |
Gets outdated Chocolatey packages on the local system | |
.DESCRIPTION | |
Returns an array of objects with the following properties: | |
- PackageName: [string] Name of the Chocolatey package. | |
- CurrentVersion: [string] Current version of the Chocolatey package. | |
- AvailableVersion: [string] Latest available version of the Chocolatey package available from your package feed(s). | |
- Pinned: [bool] True if the package is currently pinned, false if not. | |
Each object also has an Upgrade method, where the only argument is a string or array of | |
additional arguments to pass to `choco upgrade`. See the examples for how to use this. | |
Upgrade returns true if the upgrade command succeeded and false if it failed. | |
.EXAMPLE | |
Get-ChocolateyOutdatedPackages | |
PackageName CurrentVersion AvailableVersion Pinned | |
----------- -------------- ---------------- ------ | |
chefdk 2.2.1 4.7.73 True | |
vscode 1.41.1 1.42.0 False | |
vscode.install 1.41.1 1.42.0 False | |
.EXAMPLE | |
Get-ChocolateyOutdatedPackages | ForEach-Object { $_.Upgrade('-y') } | |
Upgrade all outdated packages, passing the -y argument to `choco upgrade` | |
#> | |
$chocoCmd = Get-Command choco, chocolatey -EA 0 | Select-Object -First 1 | |
if( -Not $chocoCmd ) { | |
Write-Error "Unable to locate chocolatey command" -EA Stop | |
return | |
} | |
# We will create an outdated package object row from each row output by `choco outdated` | |
@( & $chocoCmd.Source outdated -r ) | Where-Object { $_ -And $_ -notmatch '\s+' } | Foreach-Object { | |
# Split the row by | | |
$pInfo = $_ -split '\|' | |
# Create and return the object, adding the Upgrade method to it. | |
# $pInfo columns correspond to the named columns of the | |
# `choco outdated` command. | |
[PSCustomObject]@{ | |
PackageName = $pInfo[0] | |
CurrentVersion = $pInfo[1] | |
AvailableVersion = $pInfo[2] | |
Pinned = $pInfo[3] -eq 'true' | |
} | Add-Member -EA 0 -Passthru -MemberType ScriptMethod -Name Upgrade -Value { | |
& @( Get-Command choco, chocolatey -EA 0 )[0].Source upgrade $This.PackageName @args | |
$LASTEXITCODE -In 0, 3010, 1641 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment