Powershell – Copying files from local server to remote server using powershell

copypowershell

Apologies if this has already answered directly or indirectly in SF.

My requirement will be as follows:

  1. Move a deployment folder that has got files and subdirectories to a remote sever.
  2. Pass credentials that will be required for the remote server.

I used 'Start-BitsTransfer' and 'copy-item' commands along with 'new-pssession' but I was not able to transfer the files successfully.
All the errors that I get is the path is not found (remote server) or something to do with authentication going bad.


Kenny's answer did solve the above problem, but now I am facing a new issue.
The below script works for the first time while connecting to the remote server.
Now, if I reuse the script by only changing the remote server to another servername, then I get the below error:

Exception calling "MapNetworkDrive" with "5" argument(s): "The local device name has a remembered connection to another network resource.

"
At line:20 char:1
+ $net.mapnetworkdrive("F:", "\NEWSERVERNAME01.COM\share\", "true", …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
"

Any idea on how to get through the above error ?

Best Answer

You can create a PSDrive

New-PSDrive -Name "Computer1" -PSProvider FileSystem -Root "\\Computer1\Folder" -Credential $cred -Scope global
cp data.bin Computer1:\

THe PSDrive only exists during your powersehll session.

If you want to tranfer data through powershell remoting see this post https://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command