访问受密码保护的网络共享(两次/第二跳限制)

时间:2019-01-08 20:21:37

标签: powershell powershell-remoting network-share

这是关于著名的双跳限制,它看起来很琐碎,并且至少有10种解决方法,但我什至找不到适合我的设置的一种。

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

我的目标是优化PC管理,例如安装软件,注册/启动服务等-我需要在所有计算机上一次执行该操作,以免每次执行50次任务。我设法相对较快地远程运行PowerShell,但是很快我就无法访问任何需要额外身份验证的网络资源(我们所有的网络共享都需要LDAP身份验证)。

到目前为止我尝试过的。

  1. 从会话中重新认证,如here所述:

    $mappedDrive = @{
        Name = "u"
        PSProvider = "FileSystem"
        Root = "\\bladefs\share2"
        Credential = 'svetlozar.draganov'
    }
    Invoke-Command -ComputerName bw33 -ScriptBlock {
        New-PSDrive @using:mappedDrive
        Get-Content -Path \\bladefs\share2\text.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 '\\bladefs\share2\text.txt' because it does not exist.
        + CategoryInfo          : ObjectNotFound: (\\bladefs\share2\text.txt:String) [Get-Content], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
        + PSComputerName        : bw33
    

    我实际上在视频波纹管上捕捉到了有效和无效的尝试: https://drive.google.com/uc?id=1HYD8p-VkLYyIExZVWO_8qgpI2kmlUDgF

    如您所见,第一次执行时一切都很好,PSDrive已成功映射,我可以到达\ bladefs \ share2网络路径,但是第二次执行时我遇到了一些错误。

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

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

    作为第一个,有时可以使用,但是再次只能使用一次,所有后续执行都会导致此错误:

    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. 使用here中所述的CredSSP:

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

    尽管这是解决此问题的最流行和最不安全的方法,但我还是决定尝试一下,因为推荐的选项不起作用。不幸的是,我也用这种方法砸了砖,这是错误:

    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 \\bladefs\sh ...
    +                         ~~~~~~~~
       + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
       + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
    

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

1 个答案:

答案 0 :(得分:0)

我前一段时间遇到了与您类似的问题,但是我加入了域。只要您具有凭据,那就不会有太大的区别。在您的示例中,您似乎并没有使用实际的PSCredential对象,这可能是您遇到的问题。如果您可以使用相同的凭据连接到远程系统,然后再返回到共享,则应该可以:

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