Reputation: 108
I have files stored on a network share \\domain.company.com\Server01\Folder1\Path
that I am looking to perform a recursive copy to a remote server \\RemoteServer1\D$\Temp\Folder1
.
Initially, I looked at using Copy-Item -Path "\\domain.company.com\Server01\Folder1\Path\*" -Destination "\\RemoteServer1\D$\Temp\Folder1" -Recurse -Force
. However, since the script is running from a machine that doesn't have administrative access to the RemoteServer, I need to pass in Credentials for the Copy-Item
.
As we know, this isn't possible due to the FileSystem which leads me to using New-PSDrive
.
So here I am writing the following code block:
New-PSDrive -Name Source -PSProvider FileSystem "\\domain.company.com\Server01\Folder1\Path" -Credential $Creds
New-PSDrive -Name Target -PSProvider FileSystem "\\RemoteServer1\D$\Temp\Folder1" -Credential $Creds
Copy-Item -Path Source:\* -Destination Target: -Recurse -Force
Remove-PSDrive Source
Remove-PSDrive Target
Now my next problem arrises in that New-PSDrive
errors when I pass in my credentials, informing me that "the network path was not found" even though I know fully that the passed in credentials have full rights to this network share and remote server.
My question here is what is the best way of copying items, folders, and items within those folders to a remote server while on my machine and when using passed in credentials for a different account.
Upvotes: 0
Views: 1078
Reputation: 108
In short, I discovered I was using the New-PSDrive
command incorrectly in which it doesn't "map" the name, but rather creates a reference. You still need to specify the full path in the Copy-Item
. See below for working solution:
New-PSDrive -Name Source -PSProvider FileSystem -Root "\\domain.company.com\Server01\Folder1\Path" | Out-Null
New-PSDrive -Name Target -PSProvider FileSystem -Root "\\RemoteServer1\D$\Temp\Folder1" -Credential $Creds | Out-Null
Copy-Item -Path "\\domain.company.com\Server01\Folder1\Path\*" -Destination "\\RemoteServer1\D$\Temp\Folder1" -Recurse -Force
Remove-PSDrive Source
Remove-PSDrive Target
Upvotes: 0