【发布时间】: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