Skip to content

Instantly share code, notes, and snippets.

@techdecline
Created June 15, 2021 05:20
Show Gist options
  • Select an option

  • Save techdecline/77706c1b431829c5a32a05de9bfea271 to your computer and use it in GitHub Desktop.

Select an option

Save techdecline/77706c1b431829c5a32a05de9bfea271 to your computer and use it in GitHub Desktop.
Update-ChocoPackage.ps1
[cmdletbinding(DefaultParameterSetName="Default",SupportsShouldProcess=$true)]
param (
[Parameter(Mandatory,ParameterSetName="ByPackageName")]
[String]$PackageName,
[Parameter(Mandatory,ParameterSetName="ByGlobalList")]
[Switch]$UpdateAll
)
function Test-Choco
{
[CmdletBinding()]
param (
)
Process
{
if (-not (Test-Path "$env:ALLUSERSPROFILE\Chocolatey\bin\choco.exe")) {
return
}
else {
Write-Verbose "The Chocolatey Executable variable was found."
return $true
}
}
}
function Get-ChocoInstalledApps {
$chocoExe = (Get-Command "choco.exe" -ErrorAction Stop | Where-Object {$_.CommandType -eq "Application"}).Source
$cmd = $chocoExe + " list --localonly"
$rtnArr = Invoke-Expression $cmd | Select-Object -SkipLast 1 | Select-Object @{name='PackageName';expression={($_ -split " ")[0]}},
@{name='PackageVersion';expression={($_ -split " ")[1]}}
return $rtnArr
}
function Get-ChocoOutdatedApps {
$chocoExe = (Get-Command "choco.exe" -ErrorAction Stop | Where-Object {$_.CommandType -eq "Application"}).Source
$cmd = $chocoExe + " outdated"
$rtnArr = Invoke-Expression $cmd | Where-Object {$_ -match "^(\w|\d).*\|.*\|.*\|.*$"} | Select-Object -Property @{name='PackageName';expression={($_ -split "\|")[0]}},
@{name='InstallVersion';expression={($_ -split "\|")[1]}},
@{name='LatestVersion';expression={($_ -split "\|")[2]}}
return $rtnArr
}
function Update-ChocoPackage {
param (
[Parameter(Mandatory)]
[string]$PackageName
)
$chocoExe = (Get-Command "choco.exe" -ErrorAction Stop | Where-Object {$_.CommandType -eq "Application"}).Source
$cmd = $chocoExe + " upgrade $PackageName -y"
$null = Invoke-Expression $cmd
return $?
}
# Start Script Execution
## Preflight
if (-not (Test-Choco)) {
Write-Warning "Chocolatey is not installed"
return 1
}
## Query installed Packages
$installedPackageArr = Get-ChocoInstalledApps
$outdatedPackageArr = Get-ChocoOutdatedApps
## Determine Execution Mode
Write-Verbose "Parameter Set Name is: $($PSCmdlet.ParameterSetName)"
switch ($PSCmdlet.ParameterSetName) {
"ByPackageName" {
Write-Verbose "Package Name is: $PackageName"
if ($installedPackageArr.PackageName -notcontains $PackageName) {
Write-Warning "The selected package is not installed"
return 1
}
elseif ($outdatedPackageArr.PackageName -notcontains $PackageName) {
Write-Verbose "The selected package is already updated"
return 0
}
else {
Write-Verbose "The selected package will be updated"
if ($PSCmdlet.ShouldProcess($PackageName)) {
$updateRtn = Update-ChocoPackage -PackageName $PackageName
return $updateRtn
}
}
}
"ByGlobalList" {
$outdatedPackageArr | ForEach-Object {
Write-Verbose "Updating Package: $($_.PackageName)"
$result = Update-ChocoPackage -PackageName $_.PackageName
if ($result) {
Write-Verbose "Update successful."
}
else {
Write-Warning "Error updating Package: $($_.PackageName)"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment