Search code examples
powershellremote-accessinvoke-commandcredssp

Need to remote access second hops via powershell with invoke-command


I struggle a lot of time regarding this problem now and asked already in another forum, which helped to approach to the solution but finally I didn't achieve it.

I "simply" need to gather information about hosts inside clouds here in our company with the help of a powershell script. You can reach the clouds via a jumphost from the local network, the jumphost is a part of the cloud as well. Then from the jumphost you can reach all cloudhosts. So I have tried this with 2 pssessions and used invoke-command (same password for jumphost and 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

But this always gives me the output of C:\ of the jumphost and not of the cloudhost.

In the other forum I was advised to use credssp or delegated sessions. CredSSP is not possible here, because I get the error that the SPN is missing in the AD-account and I'm not allowed to add one and the delegated sessions don't help me, because I have admin rights on the jumphost and the cloudhost and don't need to delegate something.

But anyway I think it's not a problem of the credentials, because I get no "Access denied" error or something else and enter-pssession to the jumphost and from there invoke-command to the cloudhost works without a problem, but I can't use this in a script. Something seems to be wrong in the logic of the nested pssessions code...

Do you have any idea to make this work proper here?

delegated admin rights: 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

Thanks a lot, Marc

By the way: I've tried to use CredSSP, because it is recommend to use it for second hops:

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

After entering this last command I get the following error:

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 

I do not know what to avtivate further to use CredSSP, anyway it would be better to make it work without CredSSP.

Ok additionally: It should work with delegated sessions, I have tried the following:

on the Jumphost:

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

And then granted access to the user 'ad\username' (Invoke and Read access). After that I was able to use the session configuration on the jumphost with the follwoing commands:

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

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

So the session was connected and I entered my other commands:

$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

Unfortunately I get the output of Get-ChildItem C:\ of the jumphost again and not the output of the cloudhost... Do you have any further idea? Is maybe the $script{} part somewhere wrong?


Solution

  • Finally it works:

    $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
    

    This means I do not need the delegated sessions or CredSSP. But anyway, it works as well by manual setting the delegated configuration on the jumphost and then adding the users who should be able to connect to the session configuration in the pop-up -ShowSecurityDescriptorUI and delete the default users "interactive user" and local administrators:

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

    If you now connect to the session configuration with a above specified user the commands will be executed in context of the user you have specified under -RunAsCredential.

    It is also working directly from the local host, but you have to use -SecurityDescriptorSddl and this requires a function which deletes the default credentials for the session configurations and adds the new credential with ACLs automatically...means a lot of work.

    Thanks a lot for the help!

    Marc