4
votes

I need to copy file from one remote server to other using powershell script.

What I have tried :-

While i use following powershell commands it's work fine.(means file copied from one server to other)

enter image description here

But while i use following script it gives error "cannot find path..." as follows

enter image description here

Actually, file is exist at that path.

I have tried to refer following stack-overflow already question-answer

I have also tried to get help using

 Get-Help Invoke-Command

Question :-

  1. How can i use "Copy-Item" command inside "Invoke-Command(Scriptblock)" in script(2) to copy file?
  2. Is there any better way to achieve this(means best practice)?
1
C:\windows.txt exists only on the calling machine?? If your command would’ve succeeded, you would’ve simply copied windows.txt from C:\ to C:\ on the remote hostDoug Maurer
@Dough Maurer yes. for testing purpose now, I have just kept windows.txt at C:\ drive of one machine (source) once command succeed file will copy at other machine(destination) C:\ drive.Harish Shisode

1 Answers

1
votes

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