chobo2 chobo2 - 1 year ago 211
PowerShell Question

Copy File Remotely with Powershell

I am writing a powershell script that I want to run from Server A.
I want to connect to Server B and copy a file to Server A as a backup.

If that can't be done then I would like to connect to Server B from Server A and copy a file to another directory in Server B.

I see they

command but I don't see how to give it a computer name.

I would have thought I could do something like

Copy-Item -ComputerName ServerB -Path C:\Programs\temp\test.txt -Destination (not sure how it would know to use ServerB or ServerA)

How can I do this?

Answer Source

Simply use the administrative shares to copy files between systems. Much easier this way.

Copy-Item -Path \\serverb\c$\programs\temp\test.txt -Destination \\servera\c$\programs\temp\test.txt;

By using UNC paths instead of local filesystem paths, you help to ensure that your script is executable from any client system with access to those UNC paths. If you use local filesystem paths, then you are cornering yourself into running the script on a specific computer.

This only works when PowerShell session runs under user who has rights to both administrative shares. I suggest to use regular network share on server B with readonly access to everyone and simply call (from Server A):

Copy-Item -Path "\\\ServerB\SharedPathToSourceFile" -Destination "$Env:USERPROFILE" -Force -PassThru -Verbose
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download