2015-09-04 130 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