I am trying to transfer a file from a local machine to a remote machine via Powershell. I am running Powershell version 3.0 and do not have the ability to upgrade Powershell.
I must a remote session with specified credentials.
Below is the code that I am using:
# $credential variable created and set elsewhere
$command = {
param( [System.IO.FileStream]$inputStream, [string]$filePath, [string]$fileName )
$writeStream = [System.IO.File]::Create( "$filePath\$fileName" );
$inputStream.Seek(0, [System.IO.SeekOrigin]::Begin);
$inputStream.CopyTo( $writeStream );
$writeStream.Close();
$writeStream.Dispose();
}
$readStream = [System.IO.File]::OpenRead( "C:\temp\tempfile.txt" );
$path = "C:\temp";
$file = "test.txt";
$session = New-PSSession -ComputerName RemoteMachineName -UseSSL -Credential $credential;
# this works when copying the file/stream locally
Invoke-Command -ArgumentList $readStream, $path, $file -ScriptBlock $command
# this does not work when copying the file/stream remotely
Invoke-Command -Session $session -ArgumentList $readStream, $path, $file -ScriptBlock $command
$readStream.Close();
$readStream.Dispose();
The code above works when attempting to copy this file locally. But when attempting to copy this file to a remote machine via a session, I get the following error:
Cannot process argument transformation on parameter 'inputStream'. Cannot convert the "System.IO.FileStream" value of type "Deserialized.System.IO.FileStream" to type "System.IO.FileStream". + CategoryInfo : InvalidData: (:) [], ParameterBindin...mationException + FullyQualifiedErrorId : ParameterArgumentTransformationError + PSComputerName : RemoteMachineName
I cannot find anywhere on the internet any reference to the class "Deserialized.System.IO.FileStream". My guess is that the stream is getting serialized as it gets transferred to the remote machine and that this is what is causing the issue.
Does anyone know how to do this properly or have a better method for transferring this file? The final file(s) are large so I cannot just read them into a byte array without running out of memory. Also, I must use the specified credentials and the transfer must be encrypted.