2015-09-04 105 views
1

现在我在这个问题上挣扎了很多时间,并且在另一个论坛上已经询问,这有助于解决方案,但最终我没有实现。需要通过powershell通过invoke-command远程访问第二跳

我“简单地”需要在powershell脚本的帮助下,在我们公司收集有关云中主机的信息。您可以通过本地网络中的一个巨大云端接入云端,云端也是云端的一部分。然后从jumphost你可以达到所有云主机。 所以我有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 

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

在另一个论坛中,我被建议使用credssp或委托会话。 CredSSP在这里是不可能的,因为我得到AD帐户中缺少SPN的错误,我不允许添加一个,委派的会话也不能帮助我,因为我拥有Jumphost的管理权限,云主机并不需要委托任何东西。

但无论如何,我认为这不是证书的问题,因为我没有得到“访问被拒绝”的错误或其他的东西,并输入pssession jumphost和从那里调用命令到云主机工作没有问题,但我不能在脚本中使用它。 嵌套的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 

我不知道该怎么进一步avtivate使用CredSSP的,反正它会更好,使其不CredSSP的工作。

好的另外: 它应与委托会议的工作,我已经试过如下:

在Jumphost:

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

然后授权访问用户的广告\用户名“ (调用和读取权限)。 之后,我能够使用的会话配置上jumphost与follwoing命令:

$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 

不幸的是我得到的输出Get-ChildItem的C:\再次跳转,而不是云主机的输出... 您有任何进一步的想法吗?也许$ script {}部分在某个地方出错了?

+0

我推荐在另一个线程上委托会话,因为我已经面对并解决了使用它们的同一种问题。从安全角度看,它们已经被证明是比CredSSP更好的解决方案。如果在跳转主机上使用Enter-PSSession,那么委托的远程会话也可以工作,但听起来你甚至没有尝试过。 – mjolinor

+0

是enter-pssession可用于jumphost,然后用invoke-command指向cloudhost。 不,我没有尝试委派的会议,因为我想如果我使用管理员帐户无论如何,为什么我必须委托另一个管理员帐户的权利?我只能使用我在上面的脚本中输入的这个管理员帐户。 – MarcT

+0

好吧,我已经得到了委托会议在这种情况下会有所帮助的信息......有没有人知道这个看起来是怎么样的? Register-PSSessionConfiguration -Name PowerShell.Session -RunAsCredential'ad \ username'-Force – MarcT

回答

3

最后,它的工作原理:

$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的新的凭证功能...指大量的工作。

非常感谢您的帮助!

Marc