Installing Chocolatey Packages Remotely With PowerShell

One “free” solution for deploying Chocolatey packages remotely is PowerShell. Using PowerShell remoting techniques, software can be deployed in parallel on many different machines at once. In this article, I will demonstrate this.

As I have written previously, Chocolatey is breath of fresh air to Windows sysadmins everywhere, as it allows for a universal way to manage and deploy software in the enterprise. As Chocolatey is best used via its CLI (command line interface) with CMD or PowerShell, there are many different methods one can use to remotely deploy software to client workstations and servers. These solutions could be Puppet, Chef and SCCM among many others.

PowerShell Remoting Example

One command that I find myself using very often is Invoke-Command. This cmdlet allows me run remote commands and scripts on many machines at once, which makes it incredibly powerful. The two main parameters I usually use are ComputerName and ScriptBlock. The ComputerName parameter requires an input of the hostname on which you want to run remote code on. It can be one host or multiple. The ScriptBlock parameter is obviously a block of code you want to run.

In this case, I want to get the status of the User Profile Service on every machine in Active Directory:

$Computers = Get-ADComputer -Filter * | Select-Object -ExpandProperty Name

 

Invoke-Command -ComputerName $Computers -ScriptBlock {

    if (Get-Service ProfSvc | Where-Object {$_.Status -eq 'Running'}) {

        Write-Output "$Env:COMPUTERNAME has ProfSvc running"

    }

}

PowerShell checks if the User Profile Service is running, and if it does, it prints the computers name to the output:

WIN-1 has ProfSvc running

WIN-2 has ProfSvc running

WIN-3 has ProfSvc running

WIN-4 has ProfSvc running

Chocolatey Install

Whether on a local machine or running on a remote PowerShell session, to install a Chocolatey package is the same command, choco install. To install a package without being prompted add the –y argument. This is something I almost always do.

Here, I want to install Firefox on my local machine:

choco install firefox googlechrome -y

Remotely Install Google Chrome

In this scenario, I need to install Google Chrome on many machines at once. I will first load the computer names into a variable in PowerShell by querying a specific OU in Active Directory (Windows 10). Then, use Invoke-Command with choco install and do some error checking to see if Chocolatey successfully installed Chrome:

$Computers = Get-ADComputer -Filter * -SearchBase "OU=Windows 10,DC=domain,DC=com" | Select-Object -ExpandProperty Name

 

Invoke-Command -ComputerName $Computers -ScriptBlock {

    choco install googlechrome -y | Out-File -FilePath C:\Windows\Temp\choco-googlechrome.txt

    if ($?) {

        Write-Output "$Env:COMPUTERNAME Successful"

    }

    else {

        Write-Output "$Env:COMPUTERNAME Failed"

    }

}

Read more at ipswitch.com

Comments are closed.