【问题标题】:PowerShell: Copying Items from Network Share to Remote Server while using CredentialsPowerShell:使用凭据时将项目从网络共享复制到远程服务器
【发布时间】: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 命令对于 SourceTarget 是否都失败?
  • 尝试映射到共享,而不是子文件夹?然后,如果可行,请使用 Get-ChildItem -Path "$psDriveName:" 并查看它的内容,然后使用 "$psDriveName\subfolder"。

标签: powershell copy-item new-psdrive


【解决方案1】:

简而言之,我发现我错误地使用了New-PSDrive 命令,它没有“映射”名称,而是创建了一个引用。您仍然需要在Copy-Item 中指定完整路径。请参阅下面的工作解决方案:

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

【讨论】:

  • 不是这样。您应该能够像在原始帖子中那样访问 Source: 和 Target: drive。 Copy-Item -Path Source:\* -Destination Target:\ -Recurse -Force 应该可以工作
  • 您是否从相同的脚本/会话中添加 PSDrive,作为您稍后尝试访问它们的脚本/会话?
  • 您的原始帖子说您的错误与驱动器的映射有关,而不是通过 Copy-Item 访问驱动器。正如 TheMadTechnician 所说,准确的错误会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 2011-10-08
  • 2010-09-07
  • 2022-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多