我无法从本地计算机访问Powershell远程会话中的服务器上的UNC路径。我可以直接在Servers Cmd提示符中使用它们。
实际上,我已登录到服务器并将UNC路径映射为本地驱动器(例如X :)。使用登录时重新连接选项。
我有一个批处理文件驻留在这个X:驱动器中,我想使用本地脚本中的invoke命令远程运行它。但是,它失败了。
它说“找不到驱动器。名称为X的驱动器不存在”。
此外,当我尝试使用scriptblock中的net use命令映射驱动器时,它也会抛出错误 - 系统错误1223 - 本机命令错误。
我使用管理员凭据登录此服务器。
任何人都可以请帮助我,我想要做的就是远程运行脚本驻留在这个UNC路径?
此外,当我将服务器中的UNC路径映射为本地驱动器时,为什么我无法在PS远程会话中使用它?
提前致谢。 TS
你真的有三件不同的事情在这里发生。
1和3.仅在以交互方式登录时映射驱动器。因此,当您远程连接到另一台计算机,映射驱动器,然后注销/断开连接时,该映射驱动器已断开连接。除了在交互式GUI用户会话中,您不能依赖于您自己不创建的映射驱动器号。在脚本或任何远程会话中,只需使用UNC路径即可 - 它更可靠。
2。当您尝试在远程PS会话中映射驱动器时,您遇到了所谓的“双跃点”问题。有一个解决方案,但你需要做额外的设置。见Double hop access to copy files without CredSSP
alroc's helpful answer很好地解释了问题并指出了通过先前配置解决问题的资源。
至于访问远程会话中的网络共享的临时解决方案(PSv3 +):
-Credential $cred
$using:cred
- 使用New-PSDrive
建立映射网络位置的会话范围的辅助驱动器。
映射驱动器后,可以访问目标位置 - 无论是通过驱动器名称,还是直接通过UNC路径。注意:这是OP他/她自己发现的方法的变体,并在对alroc的答案的评论中简要讨论,除了使用New-PSDrive
而不是net use
,这消除了以纯文本形式检索密码的需要。
以下示例代码演示了如何从远程会话内部的网络共享运行脚本:
# A sample script to run from a network share on a remote computer.
$script = '\\server-001\install\Install-Agent.ps1'
# A sample target computer.
$computer = 'ws-002'
# Obtain the credentials for the remote session and store them in a variable.
$cred = Get-Credential $env:USERNAME
Invoke-Command -ComputerName $computer -Credential $cred {
# Map the target network share as a dummy PS drive using the passed-through
# credentials.
# You may - but needn't - use this drive; the mere fact of having established
# a drive with valid credentials makes the network location accessible in the
# session, even with direct use of UNC paths.
$null = New-PSDrive -Credential $using:cred dummy -Root (Split-Path -Parent $using:script) -PSProvider FileSystem
# Invoke the script via its UNC path.
& $using:script
}
您可以尝试通过调用WmiMethod来打开脚本:
$cmd = "CMD.EXE /c *\\\servername\somefolder\yourscript*.cmd"
Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ComputerName *servername*
请注意,这将在服务器上运行脚本。你可以做更多的事情,比如远程安装软件包(在远程机器上本地复制软件包之后)。
希望这有帮助!