0

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.

  • 1
    Please provide the exact error you receive (with sensitive details changed/removed as needed). Does the `New-PSDrive` command fail for both `Source` and `Target`? – TheMadTechnician Sep 23 '21 at 21:12
  • Try mapping to the share, not a subfolder? Then if that works, use Get-ChildItem -Path "$psDriveName:" and see what it says, then "$psDriveName\subfolder". – DarkMoon Sep 23 '21 at 22:03

1 Answers1

0

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
  • 1
    Not so. You should be able to access Source: and Target: drive just as you were doing it in your original post. `Copy-Item -Path Source:\* -Destination Target:\ -Recurse -Force` should work – Daniel Sep 27 '21 at 16:33
  • Are you adding the PSDrives from the same script/session as the one you are trying to access them from later? – Daniel Sep 27 '21 at 16:40
  • Your original post said your error was with the mapping of the drive, not accessing the drive via Copy-Item. As TheMadTechnician said, exact errors help. – DarkMoon Sep 27 '21 at 20:47