Copying across multiple remoting sessions

I needed to copy the same file to multiple machines so I tried this:

$computers = ‘W16CN01’, ‘W16CN02’

$s = New-PSSession -ComputerName $computers

Copy-Item -Path ‘C:\Source\Windows 2016 TP5\Cumulative Update for Windows Server 2016 Technical Preview 5 (KB3163016)\AMD64-all-windows10.0-kb3163016-x64_83d6e9bca94a64a5d9be3d81cdb182e540285848.msu’ -Destination C:\Scripts -ToSession $s

Remove-PSSession –Session $s

It failed with:

Copy-Item : Cannot convert ‘System.Object[]’ to the type
‘System.Management.Automation.Runspaces.PSSession’ required by parameter
‘ToSession’. Specified method is not supported.
At line:5 char:236
+ … a5d9be3d81cdb182e540285848.msu’ -Destination C:\Scripts -ToSession $s
+                                                                        ~~
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindi
   ngException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Comma
   nds.CopyItemCommand

If you look at the syntax

Get-Command Copy-Item -Syntax

Copy-Item [-Path] <string[]> [[-Destination] <string>] [-Container]
[-Force] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>]
[-Recurse] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm]
[-UseTransaction] [-FromSession <PSSession>] [-ToSession <PSSession>]
[<CommonParameters>]

Copy-Item [[-Destination] <string>] -LiteralPath <string[]> [-Container]
[-Force] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>]
[-Recurse] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm]
[-UseTransaction] [-FromSession <PSSession>] [-ToSession <PSSession>]
[<CommonParameters>]

the ToSession and FromSession parameters only take a single session as their value.

Modify the code to

$computers = ‘W16CN01’, ‘W16CN02’

$sessions = New-PSSession -ComputerName $computers

foreach ($session in $sessions) {
Copy-Item -Path ‘C:\Source\Windows 2016 TP5\Cumulative Update for Windows Server 2016 Technical Preview 5 (KB3163016)\AMD64-all-windows10.0-kb3163016-x64_83d6e9bca94a64a5d9be3d81cdb182e540285848.msu’ -Destination C:\Scripts -ToSession $session
}

Remove-PSSession -Session $sessions

And it works

This entry was posted in PowerShell v5, Windows Server 2016. Bookmark the permalink.

One Response to Copying across multiple remoting sessions

  1. Andrew K. says:

    Thanks! Great post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s