简体   繁体   English

无法在 Powershell 远程会话中访问 UNC 路径

[英]Unable to access UNC Paths in Powershell remote session

I am unable to access the UNC paths on my servers in a Powershell remote session from my local machine.我无法在本地计算机的 Powershell 远程会话中访问服务器上的 UNC 路径。 I am able to use them from Servers Cmd prompt directly.我可以直接从 Servers Cmd 提示中使用它们。

Actually, I have logged into the server and mapped a UNC path as local drive (say X:).实际上,我已经登录到服务器并将 UNC 路径映射为本地驱动器(比如 X:)。 Used Reconnect on Login option.使用重新连接登录选项。

I have a batch file which resides in this X: drive, I want to run it remotely using invoke command from my local script.我有一个位于此 X: 驱动器中的批处理文件,我想使用本地脚本中的 invoke 命令远程运行它。 But, it fails.但是,它失败了。

It says "Cannot find drive. A drive with name X doesn't exist".它说“找不到驱动器。名称为 X 的驱动器不存在”。

Also, when I try to map drive using net use command in the scriptblock then also it throws an error - System error 1223 - Native Command Error.此外,当我尝试在脚本块中使用 net use 命令映射驱动器时,它也会引发错误 - 系统错误 1223 - 本机命令错误。

I use administrator credentials to log into this server.我使用管理员凭据登录到此服务器。

Can anyone please help me on this, all i want to do is to run the script remotely which resides on this UNC path ?任何人都可以帮我解决这个问题,我想做的就是远程运行驻留在此 UNC 路径上的脚本?

Also, when I map a UNC Path in the server as a local drive, why am I unable to use it in a PS remote session ?另外,当我将服务器中的 UNC 路径映射为本地驱动器时,为什么我无法在 PS 远程会话中使用它?

Thanks in Advance.提前致谢。 TS TS

You've really got 3 different things going on here.你真的有 3 种不同的事情在这里发生。

1 & 3. Drives are only mapped when you log on interactively. 1 & 3. 只有在您交互登录时才会映射驱动器。 So when you remoted into the other computer, mapped a drive, and then logged off/disconnected, that mapped drive was disconnected.因此,当您远程访问另一台计算机,映射驱动器,然后注销/断开连接时,该映射驱动器已断开连接。 Except in interactive GUI user sessions, you cannot depend upon a mapped drive letter that you don't create yourself.除了在交互式 GUI 用户会话中,您不能依赖不是您自己创建的映射驱动器号。 Within scripts or any remote session, just use UNC paths for everything - it's more reliable.在脚本或任何远程会话中,只需对所有内容使用 UNC 路径 - 它更可靠。

2 . 2 . When you attempt to map the drive in the remote PS session, you're encountering what's known as the "double hop" problem.当您尝试在远程 PS 会话中映射驱动器时,您会遇到所谓的“双跳”问题。 There is a solution to this, but there's extra setup you have to do.有一个解决方案,但您必须进行额外的设置。 See Double hop access to copy files without CredSSP请参阅双跳访问以在没有 CredSSP 的情况下复制文件

alroc's helpful answer explains the problem well and points to resources for solving it through prior configuration . alroc 的有用回答很好地解释了问题,并指出了通过预先配置解决问题的资源。

  • An explanation of the underlying problem, the infamous Kerberos double-hop problem , as well as an overview of available solutions can be found in this blog post .这篇博文中可以找到对潜在问题的解释,臭名昭著的 Kerberos双跳问题,以及可用解决方案概述

As for an ad hoc solution for accessing a network share in a remote session (PSv3+) :至于在远程会话 (PSv3+) 中访问网络共享的临时解决方案

  • Pass the credentials for the session via a variable ;通过变量传递会话凭据; eg, -Credential $cred例如, -Credential $cred

  • Then use these credentials inside the session too - eg, as $using:cred - to establish a session-scoped auxiliary drive that maps the network location, using New-PSDrive .然后在会话中也使用这些凭据 - 例如,作为$using:cred - 使用New-PSDrive建立映射网络位置的会话范围辅助驱动器。
    Once the drive has been mapped, the target location is accessible - whether via the drive name, or directly by UNC path.映射驱动器后,即可访问目标位置 - 无论是通过驱动器名称,还是直接通过 UNC 路径。

Note: This is a variation of the approach discovered by the OP him/herself and discussed briefly in a comment on alroc's answer, except that New-PSDrive is used rather than net use , which obviates the need for retrieving the password as plain text.注意:这是 OP 他/她自己发现的方法的一种变体,并在对 alroc 答案的评论中进行了简要讨论,除了使用New-PSDrive而不是net use ,这避免了以纯文本形式检索密码的需要。

The following sample code demonstrates running a script from a network share from inside a remote session:以下示例代码演示了从远程会话内部的网络共享运行脚本:

# 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 -Name dummy -Root (Split-Path -Parent $using:script) -PSProvider FileSystem
  # Invoke the script via its UNC path.
  & $using:script
}

Note:笔记:

  • $null = ... suppresses New-PSDrive 's output (it outputs an object describing the newly created drive). $null = ...抑制New-PSDrive的输出(它输出一个描述新创建的驱动器的对象)。

  • Since the drives created by New-PSDrive are not persistent (except if you pass -Persist ), there's no need to explicitly remove the dummy drive again, but you can do so with Remove-PSDrive .由于New-PSDrive创建的驱动器不是永久性的(除非您通过-Persist ),因此无需再次明确删除虚拟驱动器,但您可以使用Remove-PSDrive这样做。

    • Also note that PS drive definitions are scoped so that only the calling scope and its descendants see the drive;还要注意 PS 驱动器定义是有作用域的,因此只有调用作用域及其后代才能看到驱动器; this enables wrapping statements in & { ... } to call them in a child scope, which means the a PS drive created inside that block will automatically go out of scope when the block is exited.这使得在& { ... }包装语句可以在作用域中调用它们,这意味着在该块内创建的 PS 驱动器将在该块退出时自动超出范围。

You can try to open the script by calling a WmiMethod :您可以尝试通过调用 WmiMethod 来打开脚本:

$cmd = "CMD.EXE /c *\\\servername\somefolder\yourscript*.cmd" 

Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ComputerName *servername*

Note that this will run the script on the server.请注意,这将在服务器上运行脚本。 You can do much more with that, like installing a package remotely (after copying the package locally on the remote machine).你可以用它做更多的事情,比如远程安装包(在远程机器上本地复制包之后)。

Hope this help!希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM