Skip to main content
added 34 characters in body
Source Link
T-Me
  • 1.9k
  • 1
  • 11
  • 23

Invoke-Command has the parameter -ArgumentList wich can be used to supply the values of local variables in the remote session. The Problem is: it's just the VALUE of the variable. No file!

What you can do:
Use Get-Content -Raw on small files to save the contant in a variable. On the target system create a New-Item with the -Value of that file. However thats not very efficent.

Example:

$txt = Get-Content -Raw -Path "C:\test\oldFile.txt" $Session = New-PSSession 127.0.0.1 Invoke-Command -Session $Session -ScriptBlock { Param($Txt) New-Item -Path c:\test\newFile.txt -Value $txt } -ArgumentList $txt #Get-PSSession | Remove-PSSession 

Result:

 Verzeichnis: C:\test # Sry german OS Mode LastWriteTime Length Name PSComputerName ---- ------------- ------ ---- -------------- -a---- 03.09.2020 12:23 658033 newFile.txt 127.0.0.1 

What you should do:
I think your use of Copy-Item -ToSession $Session is the right way to do it. It's litteraly made just for your purpose. The downside is, that the target directory needs to exist. But you need a PSSession for both cmdlets anyway. So you can use Invoke-Command with the same PSSession. First Create a PSSession. Use Invoke-Command to create you directory. Then use Copy-Item to move your file to the right place. Finally you can use Invoke-Command to do some finishing steps. And don't forget to Remove-PSSession when you are done:

$DestinationPath = "C:\test" Invoke-Command -Session $Session -ScriptBlock { Param($Destination) New-Item -Path $Destination -ItemType Directory } -ArgumentList $DestinationPath Copy-Item -Path "C:\test\oldFile.txt" -ToSession $Session -Destination "c:\test\newFile.txt" Invoke-Command -Session $Session -ScriptBlock { write-host "Do some stuff" } $Session | Remove-PSSession 

Invoke-Command has the parameter -ArgumentList wich can be used to supply the values of local variables in the remote session. The Problem is: it's just the VALUE of the variable. No file!

What you can do:
Use Get-Content -Raw on small files to save the contant in a variable. On the target system create a New-Item with the -Value of that file. However thats not very efficent.

Example:

$txt = Get-Content -Raw -Path "C:\test\oldFile.txt" $Session = New-PSSession 127.0.0.1 Invoke-Command -Session $Session -ScriptBlock { Param($Txt) New-Item -Path c:\test\newFile.txt -Value $txt } -ArgumentList $txt #Get-PSSession | Remove-PSSession 

Result:

 Verzeichnis: C:\test # Sry german OS Mode LastWriteTime Length Name PSComputerName ---- ------------- ------ ---- -------------- -a---- 03.09.2020 12:23 658033 newFile.txt 127.0.0.1 

What you should do:
I think your use of Copy-Item -ToSession $Session is the right way to do it. It's litteraly made just for your purpose. The downside is, that the target directory needs to exist. But you need a PSSession for both cmdlets anyway. So you can use Invoke-Command with the same PSSession. First Create a PSSession. Use Invoke-Command to create you directory. Then use Copy-Item to move your file to the right place. Finally you can use Invoke-Command to do some finishing steps. And don't forget to Remove-PSSession when you are done:

Invoke-Command -Session $Session -ScriptBlock { Param($Destination) New-Item -Path $Destination -ItemType Directory } -ArgumentList $DestinationPath Copy-Item -Path "C:\test\oldFile.txt" -ToSession $Session -Destination "c:\test\newFile.txt" Invoke-Command -Session $Session -ScriptBlock { write-host "Do some stuff" } $Session | Remove-PSSession 

Invoke-Command has the parameter -ArgumentList wich can be used to supply the values of local variables in the remote session. The Problem is: it's just the VALUE of the variable. No file!

What you can do:
Use Get-Content -Raw on small files to save the contant in a variable. On the target system create a New-Item with the -Value of that file. However thats not very efficent.

Example:

$txt = Get-Content -Raw -Path "C:\test\oldFile.txt" $Session = New-PSSession 127.0.0.1 Invoke-Command -Session $Session -ScriptBlock { Param($Txt) New-Item -Path c:\test\newFile.txt -Value $txt } -ArgumentList $txt #Get-PSSession | Remove-PSSession 

Result:

 Verzeichnis: C:\test # Sry german OS Mode LastWriteTime Length Name PSComputerName ---- ------------- ------ ---- -------------- -a---- 03.09.2020 12:23 658033 newFile.txt 127.0.0.1 

What you should do:
I think your use of Copy-Item -ToSession $Session is the right way to do it. It's litteraly made just for your purpose. The downside is, that the target directory needs to exist. But you need a PSSession for both cmdlets anyway. So you can use Invoke-Command with the same PSSession. First Create a PSSession. Use Invoke-Command to create you directory. Then use Copy-Item to move your file to the right place. Finally you can use Invoke-Command to do some finishing steps. And don't forget to Remove-PSSession when you are done:

$DestinationPath = "C:\test" Invoke-Command -Session $Session -ScriptBlock { Param($Destination) New-Item -Path $Destination -ItemType Directory } -ArgumentList $DestinationPath Copy-Item -Path "C:\test\oldFile.txt" -ToSession $Session -Destination "c:\test\newFile.txt" Invoke-Command -Session $Session -ScriptBlock { write-host "Do some stuff" } $Session | Remove-PSSession 
Source Link
T-Me
  • 1.9k
  • 1
  • 11
  • 23

Invoke-Command has the parameter -ArgumentList wich can be used to supply the values of local variables in the remote session. The Problem is: it's just the VALUE of the variable. No file!

What you can do:
Use Get-Content -Raw on small files to save the contant in a variable. On the target system create a New-Item with the -Value of that file. However thats not very efficent.

Example:

$txt = Get-Content -Raw -Path "C:\test\oldFile.txt" $Session = New-PSSession 127.0.0.1 Invoke-Command -Session $Session -ScriptBlock { Param($Txt) New-Item -Path c:\test\newFile.txt -Value $txt } -ArgumentList $txt #Get-PSSession | Remove-PSSession 

Result:

 Verzeichnis: C:\test # Sry german OS Mode LastWriteTime Length Name PSComputerName ---- ------------- ------ ---- -------------- -a---- 03.09.2020 12:23 658033 newFile.txt 127.0.0.1 

What you should do:
I think your use of Copy-Item -ToSession $Session is the right way to do it. It's litteraly made just for your purpose. The downside is, that the target directory needs to exist. But you need a PSSession for both cmdlets anyway. So you can use Invoke-Command with the same PSSession. First Create a PSSession. Use Invoke-Command to create you directory. Then use Copy-Item to move your file to the right place. Finally you can use Invoke-Command to do some finishing steps. And don't forget to Remove-PSSession when you are done:

Invoke-Command -Session $Session -ScriptBlock { Param($Destination) New-Item -Path $Destination -ItemType Directory } -ArgumentList $DestinationPath Copy-Item -Path "C:\test\oldFile.txt" -ToSession $Session -Destination "c:\test\newFile.txt" Invoke-Command -Session $Session -ScriptBlock { write-host "Do some stuff" } $Session | Remove-PSSession