【发布时间】:2021-09-23 20:15:52
【问题描述】:
我有文件存储在网络共享 \\domain.company.com\Server01\Folder1\Path 上,我希望将其递归复制到远程服务器 \\RemoteServer1\D$\Temp\Folder1。
最初,我考虑使用Copy-Item -Path "\\domain.company.com\Server01\Folder1\Path\*" -Destination "\\RemoteServer1\D$\Temp\Folder1" -Recurse -Force。但是,由于脚本是从对 RemoteServer 没有管理权限的机器上运行的,所以我需要传入 Copy-Item 的凭据。
正如我们所知,这是不可能的,因为文件系统导致我使用New-PSDrive。
所以我在这里写下面的代码块:
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
现在我的下一个问题是在我传递凭据时出现New-PSDrive 错误,尽管我完全知道传入的凭据对此网络共享具有完全权限,但通知我“找不到网络路径”并且远程服务器。
我的问题是,在我的机器上以及为不同帐户使用传入的凭据时,将这些文件夹中的项目、文件夹和项目复制到远程服务器的最佳方法是什么。
【问题讨论】:
-
请提供您收到的确切错误(根据需要更改/删除敏感细节)。
New-PSDrive命令对于Source和Target是否都失败? -
尝试映射到共享,而不是子文件夹?然后,如果可行,请使用 Get-ChildItem -Path "$psDriveName:" 并查看它的内容,然后使用 "$psDriveName\subfolder"。
标签: powershell copy-item new-psdrive