1

I'm trying to copy files from serverA to serverB using Powershell. Both servers belong to our hosting provider so copying files from A to B is very fast compared to copying files from my local box to either server. I figured I could use Powershell to run a remote command on serverA and copy the files to serverB. This is what I came up with:

$SourceServerName = "serverA"
$SourceContentPath = "\\serverA\c$\testSrc"
$DestinationContentPath = "\\serverB\c$\testDest"

Invoke-Command -ComputerName $SourceServerName -ScriptBlock {param ($SourcePath,$InstallPath) 
                Copy-Item -Path $SourcePath\* -Destination $InstallPath -Recurse
            } -ArgumentList $SourceContentPath, $DestinationContentPath

But I get an error "System.Management.Automation.RemoteException: Access to the path 'testDest' is denied.

I'm an admin and WinRM is configured properly on both boxes. If I try to copy files remotely inside the same server (i.e. from \\serverA\c$\testSrc to \\serverA\c$\testDest) everything works fine.

What's the proper way to do this?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
oscarmorasu
  • 901
  • 3
  • 11
  • 28
  • The account which runs powershell should be admin of both boxes. You should also check that c:\testDest on serverB has good permissions for the account you are using. – Mat M Oct 29 '12 at 21:33

2 Answers2

0

Invoke-Command is executed under your current user (user you are currently logged into your machine). You should set -Credential parameter of your Invoke-Command to the admin user of target system

Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52
0

The cmdlet on Server A is not executed as you.

Two possible solutions:

Easiest:

$SourceServerName = "serverA"
$SourceContentPath = "\\serverA\c$\testSrc"
$DestinationContentPath = "\\serverB\c$\testDest"
$cred = Get-Credential # input your username and password of serverB here

Invoke-Command -ComputerName $SourceServerName -ScriptBlock {param ($SourcePath,$InstallPath, $cred) 
            Copy-Item -Path $SourcePath\* -Destination $InstallPath -Recurse -Credential $cred
        } -ArgumentList $SourceContentPath, $DestinationContentPath, $cred

Domain User Solution:

If you are a domain user, and could control these two machines you could just enable CredSSP in PowerShell via Enable-WSManCredSSP on both your local computer and serverA, then add the -Authentication CredSSP in Invoke-Command.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Jackie
  • 2,476
  • 17
  • 20