Tuesday, September 13, 2016

Using Powershell and Chocolatey to monitor for new available packages

Using a little bit of Powershell and some Chocolatey, we can easily monitor to see if there are new packages available on Chocolatey's main feed, and if so, send an email notification.

So here is the use case. You use Chocolatey to help deploy packages in your windows environment, and you typical use the Chocolatey public repository to pull down packages and recompile to your hosted NuGet repository. Wouldn't it be nice to get notified when a new Chocolatey package is available in the public repository?

We have a test windows workstation with Chocolatey installed. It has all of the normal Chocolatey packages we deploy (such as firefox, chrome, skype etc) already installed. Once a day I want to run "choco upgrade all -noop" to see if any packages that are installed have an upgrade available in the Chocolatey repository. If there is a new package available it upgrades it on the test machine and then sends me an email.

Here is the very simple script:

## Invoke Chocolatey remotely on TestComputer in -noop mode
$a = invoke-command -computername TestComputer -ScriptBlock {choco upgrade all -y -noop}

##Check the output of Chocolatey to see if "Can Upgrade:" is there adding -Context to capture up to 60 lines of text. This is an indicator that there are packages that can be upgraded.
$output = $a | Select-String -Pattern "Can upgrade:" -Context 0,60

##If "Can Upgrade" is found upgrade the packages and send an email to sysadmin
if ($output)
$PackagesToUpgrade = invoke-command -computername TestComputer   -ScriptBlock { choco upgrade all -y -ignorechecksum }
Send-MailMessage -Body "$output `n`n` Choco output: `n`n` $PackagesToUpgrade" -To "sysadmin@domain.com" -From "smtpsender@domain.com" -SmtpServer smtp.domain.com -Subject "Choco upgrade available"

No comments:

Post a Comment

Recompiling Chocolatey packages

Chocolatey is a very popular tool among system administrators as it helps automate package management for Windows. Since Chocolatey can be u...