2

I am attempting to copy a file over a network from a Windows 2012r2 server running PowerShell 5.0 to a 2008 r2 server running PowerShell 2.0. I am attempting to do this using the Copy-Item command. The script that executes the command is:

$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($userName, $secPassword)
$sessionOptions = New-PSSessionOption -SkipCACheck -SkipCNCheck

$session = New-PSSession -Computername $computerName -SessionOption $sessionOptions -Credential $cred 

Copy-Item -Path $source -Destination $destination -ToSession $session

When executing the command, I receive the following error:

Copy-Item : Unable to index into an object of type System.IO.FileInfo. At C:\Users\pmartin.CLUBSOFTINC\Documents\Repos\Test\PowerShell\test.ps1:21 char:1 + Copy-Item -Path $source -Destination $destination -ToSession $session + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (0:Int32) [Copy-Item], RuntimeException + FullyQualifiedErrorId : CannotIndex

After doing some research, it appears this is caused by a compatibility issue in PSv2 where returning a value at index [0] from a non-array element does not return the element (as compared to PSv5 which would return the element).

Is there a workaround for this issue? If so, how can I implement it within my script?

Preston Martin
  • 183
  • 1
  • 7
  • Is it possible to update the version of PowerShell on the 2008 R2 server? – Todd Wilcox Jan 10 '18 at 21:08
  • 1
    @ToddWilcox Not at the moment, otherwise I agree, that would be the ideal solution. This command is run on a multitude of servers (via a CI deployment system) that are being migrated to Azure VMs, and right now, there is no effort to upgrade the existing data center VMs. – Preston Martin Jan 10 '18 at 21:13
  • you could try using Start-Bits transfer instead. this may help https://social.technet.microsoft.com/Forums/windowsserver/en-US/4668f8ce-61e2-4a2a-a4f4-dad29a833187/use-bits-to-copy-subfolders?forum=winservergen – Owain Esau Jan 31 '18 at 04:36
  • Link to open issue: https://github.com/PowerShell/PowerShell/issues/2469 – Der_Meister May 31 '18 at 15:53

0 Answers0