Thursday, March 23, 2017

Installing software packages remotely using Chocolatey and PowerShell

The capability to install software remotely on many machines at once is still one of the most magical things a System Administrator can do. Nowadays we take it for granted due to all of the different tools we can use to do this, but it is still one of coolest automated tasks in my opinion.

Today, we will use two tools to accomplish this, PowerShell and Chocolatey. Both automate a lot of tasks on their own, but combining them gives an admin even more power and agility to push out software quickly and easily.

In this example I have three Windows workstations in an Active Directory domain, and I would like to push Firefox to all three at the same time. To do this we will use the PowerShell "Invoke-Command" which allows us to create remote PowerShell sessions to many computers simultaneously. From there we use the choco install command on each workstation which will tell Chocolatey to install Firefox.

First we open PowerShell from our control machine. I will create an array in PowerShell for the hostnames of the workstations I want to install Firefox on.

$workstations = ("computer1","computer2","computer3")

Next, lets run the Invoke-Command in PowerShell using the $workstations variable in -ComputerName. You will notice that we pipe choco install firefox -y to Select-String. This is because we want to parse only the result of the installation, specifically the line that includes "Chocolatey installed". This line will provide if the package was successful or not. Note that you may want to use the -Credential parameter in Invoke-Command if the account you are logged into on your control machine does not have local administrator access to the workstations. Then we use Select-Object to display the installation result for each machine and it's computer name in PowerShell.

Invoke-Command -ComputerName $workstations -ScriptBlock {choco install firefox -y | Select-String -Pattern "Chocolatey installed" } | select PSComputername,Line

After running Invoke-Command we are greeted with success. Each machine successfully installed Firefox.

PSComputerName Line
-------------- ----
computer1      Chocolatey installed 1/1 packages. 0 packages failed.
computer2      Chocolatey installed 1/1 packages. 0 packages failed.
computer3      Chocolatey installed 1/1 packages. 0 packages failed.



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...