Using the PowerShell version 5 Preview OneGet Module with PowerShell Remoting

I’m picking up where I left off in my last blog article. Adobe Reader, ImgBurn, and WinRAR have been installed on a Windows 8.1 Enterprise edition computer named PC03. All of the examples shown in this blog article are being run from the PC03 computer.

I’ll use a little trick I showed you a few blog articles ago and store the names of the installed packages in a variable and return them at the same time, all in a one-liner:

($software = Get-Package | Select-Object -ExpandProperty Name)

2014-04-07_20-58-22.png

I’ll create a PSSession to the computers named PC04, PC05, and PC06, store them in a variable named $Session (technically, it’s named Session) and return a list of the sessions all in a one-liner with the same trick I used in the previous command:

($Session = New-PSSession -ComputerName pc04, pc05, pc06)

2014-04-07_20-43-41.png

PC04, PC05, and PC06 all run Windows 8.1 Enterprise edition, have the preview version of PowerShell version 5 installed, their script execution policy is set to RemoteSigned, and PowerShell remoting has been enabled on them. One of the cmdlets from the OneGet module has also been previous run on them to trigger installing the NuGet Package Manager, although no packages have previously been installed on them.

Note: The one package source (Chocolatey) that’s included by default with the preview version of PowerShell verison 5 isn’t trusted:

Get-PackageSource

2014-04-07_22-01-21.png

This means that by default you’ll be prompted as shown in the following example when installing software from that package source:

Find-Package -Name AdobeReader, ImgBurn, WinRAR |
Install-Package

2014-04-07_20-37-25.png

Since I’ll be installing the software on the three remote computers using PowerShell remoting, I’ll be unable to answer those interactive questions. There are two options to prevent being prompted, remove the Chocolatey package source and re-add it as a trusted package source, or simply use the -Force parameter with the Install-Package cmdlet.

I’ll use the -Force parameter since it’s the easier solution to this problem and it doesn’t permanently make changes to the package source settings:

Invoke-Command -Session $Session {Install-Package -Name $Using:software -Force}

2014-04-07_21-01-43.png

In the previous command, I’ve taken advantage of the Using scope modifier that was introduced in PowerShell version 3. For more information about the Using scope modifier see the about_Scopes and about_Remote_Variables help topics in PowerShell.

Success! All three of the computers (PC04, PC05, and PC06) now have the same packages installed that are installed on PC03 (Adobe Reader, ImgBurn, and WinRAR):

2014-04-07_21-04-02.png

ยต