4

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)?
2
  • 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 host Commented Sep 2, 2020 at 14:12
  • @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. Commented Sep 2, 2020 at 15:49

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.