需要通过 powershell 使用 invoke-command 远程访问第二个跃点

Posted

技术标签:

【中文标题】需要通过 powershell 使用 invoke-command 远程访问第二个跃点【英文标题】:Need to remote access second hops via powershell with invoke-command 【发布时间】:2015-09-04 06:46:14 【问题描述】:

我现在在这个问题上挣扎了很多时间,并且已经在另一个论坛上问过,这有助于接近解决方案,但最后我没有实现。

我“只是”需要借助 powershell 脚本在我们公司的云内部收集有关主机的信息。您可以通过本地网络的 jumphost 访问云,jumphost 也是云的一部分。然后从 jumphost 你可以到达所有的 cloudhosts。 所以我用 2 个 pssession 尝试了这个并使用了调用命令(jumphost 和 cloudhost 的密码相同):

$cred = Get-Credential ad\username -Message "Please insert the password for the jumphost and the cloudhost"

$session = New-PSSession -ComputerName jumphost -Credential $cred

$cldhost = Read-Host "Please insert the name of the cloudhost"

$script = 
    Param (
        $Credential,
        $cloudhost
    )
    $ses = New-PSSession -ComputerName $cloudhost -Credential $Credential
    Invoke-Command -Session $ses -ScriptBlock  Get-ChildItem C:\ 
    Remove-PSSession $ses


Invoke-Command -Session $session -ScriptBlock $script -ArgumentList $cred, $cldhost
Remove-PSSession $session

但这总是给我 Jumphost 的 C:\ 而不是 cloudhost 的输出。

在另一个论坛中,有人建议我使用 credssp 或委派会话。 此处无法使用 CredSSP,因为我收到错误消息,即 AD 帐户中缺少 SPN,并且我不允许添加一个,并且委派的会话对我没有帮助,因为我对 jumphost 和cloudhost,不需要委托任何东西。

但无论如何,我认为这不是凭据的问题,因为我没有收到“拒绝访问”错误或其他错误,并且输入-pssession 到 jumphost 并从那里调用命令到 cloudhost 没有问题,但我不能在脚本中使用它。 嵌套 pssessions 代码的逻辑似乎有问题...

您有什么想法可以在这里正常工作吗?

委派的管理员权限: http://blogs.technet.com/b/heyscriptingguy/archive/2014/04/03/use-delegated-administration-and-proxy-functions.aspx

CredSSP: http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/14/enable-powershell-quot-second-hop-quot-functionality-with-credssp.aspx

非常感谢, 马克

顺便说一句: 我尝试使用 CredSSP,因为建议将其用于第二跳:

Enable-WSManCredSSP -Role Client -DelegateComputer jumphost -Force

$cred = Get-Credential ad\username -Message "Please insert the password for the jumphost and the cloudhost"
$session = New-PSSession -ComputerName jumphost -Credential $cred
Invoke-Command -Session $session -ScriptBlock Enable-WSManCredSSP -Role Server –Force; Set-Item wsman:\localhost\client\trustedhosts -value localcomputer -Force; Restart-Service winrm -Force

$session2 = new-PSSession -ComputerName jumphost -Credential $cred -Authentication Credssp

输入最后一条命令后,出现以下错误:

The WinRM client cannot 
process the request. A computer policy does not allow the delegation of the user credentials to the target computer because the 
computer is not trusted. The identity of the target computer can be verified if you configure the WSMAN service to use a valid 
certificate using the following command: winrm set winrm/config/service '@CertificateThumbprint="<thumbprint>"'  Or you can 
check the Event Viewer for an event that specifies that the following SPN could not be created: WSMAN/<computerFQDN>. If you find 
this event, you can manually create the SPN using setspn.exe .  If the SPN exists, but CredSSP cannot use Kerberos to validate 
the identity of the target computer and you still want to allow the delegation of the user credentials to the target computer, 
use gpedit.msc and look at the following policy: Computer Configuration -> Administrative Templates -> System -> Credentials 
Delegation -> Allow Fresh Credentials with NTLM-only Server Authentication.  Verify that it is enabled and configured with an SPN 
appropriate for the target computer. For example, for a target computer name "myserver.domain.com", the SPN can be one of the 
following: WSMAN/myserver.domain.com or WSMAN/*.domain.com. Try the request again after these changes. Weitere Informationen 
finden Sie im Hilfethema "about_Remote_Troubleshooting".
In Zeile:1 Zeichen:13
+ $session2 = new-PSSession -ComputerName jumphost -Credential $cred -Aut ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportE 
   xception
    + FullyQualifiedErrorId : -2144108124,PSSessionOpenFailed 

我不知道如何进一步使用 CredSSP,无论如何最好在没有 CredSSP 的情况下使其工作。

另外确定: 它应该适用于委派会话,我尝试了以下方法:

在 Jumphost 上:

Register-PSSessionConfiguration -Name PowerShell.Session -SessionType DefaultRemoteShell -AccessMode Remote -RunAsCredential 'ad\username' -ShowSecurityDescriptorUI –Force

然后授予用户“ad\username”的访问权限(调用和读取访问权限)。 之后,我可以通过以下命令在 jumphost 上使用会话配置:

$cred = Get-Credential ad\username -Message "Please enter the password for jumphost" 

$session = New-PSSession -ConfigurationName PowerShell.Session -ComputerName jumphost -Credential $cred

因此会话已连接,我输入了其他命令:

$cldhost = Read-Host "Please enter the cloudhost name"

$script = 
    Param (
        $Credential,
        $Hostname2
    )
    $ses = New-PSSession -ComputerName $Hostname2 -Credential $Credential
    Invoke-Command -Session $ses -ScriptBlock  Get-ChildItem C:\ 
    Remove-PSSession $ses


Invoke-Command -Session $session -ScriptBlock $script -ArgumentList $cred, $cldhost
Remove-PSSession $session

不幸的是,我再次得到了 jumphost 的 Get-ChildItem C:\ 的输出,而不是 cloudhost 的输出...... 你有什么进一步的想法吗?可能是 $script 部分有问题吗?

【问题讨论】:

我推荐了另一个线程上的委派会话,因为我使用它们遇到并解决了同样的问题。从安全角度来看,它们已被证明是比 CredSSP 更好的解决方案。如果在跳转主机上使用 Enter-PSSession 有效,那么委托的远程会话也应该有效,但听起来你甚至没有尝试过。 是 enter-pssession 对 jumphost 起作用,然后使用 invoke-command 对 cloudhost 起作用。不,我没有尝试委派会话,因为我想如果我仍然使用管理员帐户,为什么我必须委派另一个管理员帐户的权限?我只能使用我在上面的脚本中输入的这个管理员帐户。 好的,我已经获得了委派会议在这种情况下会有所帮助的信息……有人知道这应该是什么样子吗?注册-PSSessionConfiguration -Name PowerShell.Session -RunAsCredential 'ad\username' –Force 【参考方案1】:

终于成功了:

$cred = Get-Credential ad\username -Message "Please insert the password for the jumphost"

$session = New-PSSession -ComputerName jumphost -Credential $cred

$cldhost = Read-Host "Please insert the cloudhost name"

$script = 
    Param (
        $Credential,
        $Hostname2
    )
    $ses = New-PSSession -ComputerName $Hostname2 -Credential $Credential
    Invoke-Command -Session $ses -ScriptBlock  Get-ChildItem C:\ 
    Remove-PSSession $ses


Invoke-Command -Session $session -ScriptBlock $script -ArgumentList $cred, $cldhost
Remove-PSSession $session

这意味着我不需要委派会话或 CredSSP。但无论如何,它也可以通过在 jumphost 上手动设置委派配置,然后在弹出 -ShowSecurityDescriptorUI 中添加应该能够连接到会话配置的用户并删除默认用户来工作“交互式用户”和本地管理员:

Register-PSSessionConfiguration -Name PowerShell.Session -SessionType DefaultRemoteShell -AccessMode Remote -RunAsCredential 'ad\username' -ShowSecurityDescriptorUI –Force

如果您现在使用上面指定的用户连接到会话配置,则命令将在您在 -RunAsCredential 下指定的用户的上下文中执行。

它也可以直接在本地主机上运行,​​但您必须使用 -SecurityDescriptorSddl,这需要一个函数来删除会话配置的默认凭据并自动添加带有 ACL 的新凭据。 ..意味着很多工作。

非常感谢您的帮助!

马克

【讨论】:

以上是关于需要通过 powershell 使用 invoke-command 远程访问第二个跃点的主要内容,如果未能解决你的问题,请参考以下文章

Powershell Invoke-Restmethod不能将生成的Authtoken用作变量

使用invoke-webrequest下载文件

Powershell 3.0 Invoke-WebRequest HTTPS 在所有请求上都失败

Powershell Invoke-WebRequest和字符编码

如何使用 Powershell 'invoke-expression' 以其他用户身份运行可执行文件

c#中的PowerShell cmd调用Invoke不返回特定用户电子邮件列表的PsObject