【问题标题】:Copy-Item from UNC path located in a different domain从位于不同域的 UNC 路径复制项目
【发布时间】:2020-03-26 07:46:53
【问题描述】:

我的任务是在分散在多个域的服务器 (2008,2012,2016) 上安装软件 (msi)。诀窍是将安装、配置文件从位于不同域中的 UNC 复制到本地的每个服务器。不同的 PowerShell 版本的行为方式不同。 最初在 Server 2016s 上,我尝试了 New-PSSession-FromSession 开关来建立与具有 UNC 路径的服务器的连接(成功)并像这样复制:

$creds = Get-Credential -Username "<domain_with_unc_share>\$($env:username)"
$unc_server_session = New-PSSession -ComputerName unc_server -Credential $creds
Copy-Item "d:\share\*.*" -Destination "c:\temp\" -FromSession $unc_server_session -Recurse

-FromSession 开关是在 PS5 中引入的,并且在 Sevrer 2016 上运行良好,但任何更低的版本都会失败。这显然不适用于 Windows 2008/2012。

然后我尝试在 Copy-Item 中只使用 UNC,如下所示:

$creds = Get-Credential -Username "<domain_with_unc_share>\$($env:username)"
Copy-Item "\\unc_server\share\*.*" -Destination "c:\temp\" -Credential $creds -Recurse

这给了我一个错误,即 -Credential 开关不能与 Copy-Item 一起使用并推迟使用 New-PSDrive

Copy-Item : Cannot retrieve the dynamic parameters for the cmdlet. The FileSystem provider supports credentials only
on the New-PSDrive cmdlet. Perform the operation again without specifying credentials.
At line:1 char:1
+ Copy-Item "\\unc_server\share\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.CopyItemCommand

然后我尝试了以下方法:

$creds = Get-Credential -Username "<domain_with_unc_share>\$($env:username)"
New-PSDrive -Name Y -PSProvider filesystem -Root "\\unc_server\share" -Credential $creds
Copy-Item "Y:\*.*" -Destination "c:\temp\" -Recurse

还是不开心,告诉我我的UNC路径不存在:

New-PSDrive : The specified drive root "\\unc_server\share"
either does not exist, or it is not a folder.
At line:1 char:1
+ New-PSDrive -Name "Y" -PSProvider filesystem -Root "\\unc_server\share ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ReadError: (Y:PSDriveInfo) [New-PSDrive], IOException
    + FullyQualifiedErrorId : DriveRootError,Microsoft.PowerShell.Commands.NewPSDriveCommand

我不知道如何安抚 PowerShell 之神,让它在 PS3、4、5 上运行。 有什么建议吗?

【问题讨论】:

  • 在文件资源管理器中,你能联系到\\unc_server\share 吗?您是否尝试过使用 IP 地址而不是 unc_server 名称?
  • 嘿西奥。我绝对可以通过资源管理器访问共享。那是我不明白的一点。我使用相同的凭据来启动 Session,适用于 Session,不适用于 New-PSDrive。

标签: powershell


【解决方案1】:

您必须使用 -Persist 开关指示您正在映射网络驱动器:

$creds = Get-Credential -Username "<domain_with_unc_share>\$($env:username)" 
New-PSDrive -Name Y -PSProvider filesystem -Root "\\unc_server\share" -Credential $creds -Persist
 Copy-Item "Y:\*.*" -Destination "c:\temp\" -Recurse

【讨论】:

  • 感谢 Wasif,即使这是临时复制会话?
  • 使用 -Persist 开关我现在得到错误:New-PSDrive:指定的网络密码不正确。我必须仔细检查我的凭据是否正确,因为如果我打开资源管理器窗口以浏览到共享,我能够进行身份验证
  • 再次检查用户名和密码。
  • 我检查了三次。密码正确。我唯一能想到的是我的密码包含特殊字符,但这应该不是问题,因为我使用 Get-Credential 来收集密码。想法?
猜你喜欢
  • 2017-01-10
  • 1970-01-01
  • 2012-06-16
  • 2017-07-28
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多