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

需要使用invoke命令通过powershell远程访问第二个跃点,powershell,remote-access,invoke-command,credssp,Powershell,Remote Access,Invoke Command,Credssp,我现在为这个问题挣扎了很多时间,并且已经在另一个论坛上提出了问题,这有助于解决问题,但最终我没有实现 我“只是”需要借助powershell脚本在我们公司的云中收集有关主机的信息。您可以从本地网络通过jumphost访问云,jumphost也是云的一部分。然后从jumphost可以访问所有cloudhosts。 因此,我尝试了2次pssessions,并使用了invoke命令(jumphost和cloudhost的密码相同): 但这总是给我jumphost的C:\输出,而不是cloudhost的

我现在为这个问题挣扎了很多时间,并且已经在另一个论坛上提出了问题,这有助于解决问题,但最终我没有实现

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

但这总是给我jumphost的C:\输出,而不是cloudhost的输出

在另一个论坛上,我被建议使用credssp或委托会议。 CredSSP在这里是不可能的,因为我得到了一个错误,即广告帐户中缺少SPN,不允许我添加SPN,并且委派的会话对我没有帮助,因为我对jumphost和cloudhost拥有管理权限,不需要委派某些内容

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

你有什么办法使这项工作在这里正常进行吗

委派的管理员权限:

CredSSP:

非常感谢, 马克

顺便说一下: 我尝试过使用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 
然后授予用户“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
因此会话已连接,我输入了其他命令:

$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{}部分可能有什么地方出错了吗?

最后它成功了:

$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下指定的用户上下文中执行

它也可以直接从本地主机工作,但您必须使用-securityDescriptorSDLL,这需要一个功能,该功能可以删除会话配置的默认凭据,并自动添加带有ACL的新凭据…这意味着大量工作

非常感谢你的帮助


Marc

我建议在另一个线程上进行授权会话,因为我已经用它们面对并解决了同样的问题。从安全角度来看,它们已被证明是比CredSSP更好的解决方案。如果在跳转主机上使用Enter-PSSession有效,那么委派的远程会话也应该有效,但听起来您甚至没有尝试过。是的,Enter-PSSession对跳转主机有效,然后使用invoke命令对云主机有效。不,我没有尝试委派会话,因为我想如果我使用管理员帐户,为什么我必须从另一个管理员帐户委派权限?我只能使用我在上面的脚本中输入的这个管理员帐户。好的,我已经得到了授权会话在这种情况下会有帮助的信息…有人知道这是什么样子吗?注册PSSessionConfiguration-名称PowerShell.Session-RunAsCredential“ad\username”-强制