访问受密码保护的网络共享(双跳/秒跳限制)



这是关于著名的双跳限制的,它看起来微不足道,至少有10个解决方案,但我甚至找不到一个适合我的设置。

这是我的环境:我在Windows上有大约50个虚拟机;10,每个虚拟机都在一个单独的硬件上运行——我们使用虚拟机是因为我们的IT人员声称它更容易维护,而物理虚拟机,我个人不喜欢虚拟机,但这并不取决于我。我们在一个非域环境中,没有Active Directory,我们使用一个工作组,每台机器都是单独管理的。

我的目标是优化PC管理,如安装软件、注册/启动服务等——我需要在所有机器上同时做到这一点,而不是每项任务执行50次。我设法相对快速地远程运行PowerShell,但很快我就无法访问任何需要额外身份验证的网络资源(我们所有的网络共享都需要LDAP身份验证)。

到目前为止我尝试了什么。

  1. 从会话重新进行身份验证,如下所述:

    $mappedDrive = @{
    Name = "u"
    PSProvider = "FileSystem"
    Root = "\bladefsshare2"
    Credential = 'svetlozar.draganov'
    }
    Invoke-Command -ComputerName bw33 -ScriptBlock {
    New-PSDrive @using:mappedDrive
    Get-Content -Path \bladefsshare2text.txt
    Get-PSDrive
    Remove-PSDrive -Name "u"
    Get-PSDrive
    } -Credential render
    

    上面的命令是通过Invoke-Command运行一个远程命令,请求两次身份验证,第一次身份验证是连接到机器bw33,然后使用New-PSDrive命令将另一次身份验证发送到与bw33已经建立的会话,以安装具有用户名和密码的网络共享。这有时在非常罕见的情况下确实有效,但我无法确定它何时有效、为什么有效,以及为什么在大多数情况下无效。尽管我执行了十几次完全相同的PowerShell脚本,但它只适用于极少数人,其余人都适用,它只是这样说:

    A specified logon session does not exist. It may already have been
    terminated
    + CategoryInfo          : InvalidOperation: (u:PSDriveInfo) [New-PSDrive], Win32Exception
    + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
    + PSComputerName        : bw33
    Cannot find path '\bladefsshare2text.txt' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (\bladefsshare2text.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
    + PSComputerName        : bw33
    

    事实上,我在下面的视频中捕捉到了一个工作和非工作的尝试:https://drive.google.com/uc?id=1HYD8p-VkLYyExZVO_8qgpI2kmlUDgF

    正如你在第一次执行中看到的那样,一切都很好。PSDrive被成功映射,我可以到达\bladefs\share2网络路径,但在第二次执行中,我遇到了一些错误。

  2. 与上面类似,但不是通过PSDrive命令映射驱动器,而是通过具有用户名和密码的NET USE命令映射驱动器。

    Invoke-Command -ComputerName bw33 -Credential render -ScriptBlock {
    net use x: \bladefsshare2 /user:svetlozar.draganov password
    Test-Path \bladefsshare2
    }
    

    与第一个一样,这有时有效,但它只起一次作用,所有后续执行都会导致此错误:

    System error 1312 has occurred.
    + CategoryInfo          : NotSpecified: (System error 1312 has occurred.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
    + PSComputerName        : bw33
    A specified logon session does not exist. It may already have been terminated.
    

    以下是另一次尝试的视频,再次捕捉到该命令的工作和非工作执行:https://drive.google.com/uc?id=1wP20sbmXMfWu4dvjsdF8REDWgNxiKAS-

  3. 使用此处描述的CredSSP:

    $session = New-PSSession -cn bw33 -Credential render -Authentication Credssp
    Invoke-Command -Session $session -ScriptBlock {Test-Path \bladefsshare2}
    

    虽然这是解决这个问题的最流行和最不安全的方法,但我决定尝试一下,因为推荐的选项不起作用。不幸的是,我也遇到了这种方法的麻烦,以下是错误:

    New-PSSession : [bw33] Connecting to remote server bw33 failed with
    the following error message : The request is not  supported. For more
    information, see the about_Remote_Troubleshooting Help topic.
    At line:1 char:12
    + $session = New-PSSession -cn bw33 -Credential render -Authentication  ...
    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : 50,PSSessionOpenFailed
    Invoke-Command : Cannot validate argument on parameter 'Session'. The
    argument is null or empty. Provide an argument that is  not null or empty,
    and then try the command again.
    At line:2 char:25
    + Invoke-Command -Session $session -ScriptBlock {Test-Path \bladefssh ...
    +                         ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
    

    分别是视频:https://drive.google.com/uc?id=10tbAq6vvRsvT-1SGqOzvPgIPcM-MT8CJ

前一段时间我遇到了一个与您类似的问题,但我有一个加入域的设置。只要你有资格证书,这应该没有多大区别。在您的示例中,您似乎没有使用实际的PSCredential对象,这可能是您的问题。如果你可以使用相同的凭据连接到远程系统,然后返回到你的共享,那么这应该有效:

$Password = Read-Host -Prompt 'Enter Password' -AsSecureString
$Credential = New-Object -TypeName PSCredential('username',$Password)
$mappedDrive = @{
Name = "u"
PSProvider = "FileSystem"
Root = "\bladefsshare2"
Credential = $Credential
}
Invoke-Command -ComputerName bw33 -Credential $Credential -ScriptBlock {
New-PSDrive @Using:mappedDrive
# Do Stuff...
Remove-PSDrive -Name "u"
}

最新更新