It looks like you are trying to copy a file using PowerShell remote access. As stated in other answers, it would be easier to use Copy-Item and / or Robocopy to copy from source to share on the destination computer.
If you want to copy a file using PowerShell remote access, you can split the file into a variable and use it in a remote script block. Sort of:
$contents = [IO.File]::ReadAllBytes( $localPath ) Invoke-Command -ComputerName <IP Address> ` -Credential $credential ` -ScriptBlock { [IO.File]::WriteAllBytes( 'C:\remotepath', $using:contents ) }
Of course, if the file you are reading is really large, it can cause the remote connection to end due to out of memory (by default, PowerShell limits remote connections to about 128 MB).
If you are stuck with PowerShell 2, you need to pass bytes as a parameter to the script block:
$contents = [IO.File]::ReadAllBytes( $localPath ) Invoke-Command -ComputerName <IP Address> ` -Credential $credential ` -ScriptBlock { param( [byte[]] $Contents ) [IO.File]::WriteAllBytes( 'C:\remotepath', $Contents) } ` -ArgumentList @( ,$contents )
And yes, you must wrap $contents in an array, passing it as a value to the -ArgumentList parameter, and it must be prefixed with a comma operator, otherwise your remote script block will only receive the first byte.
source share