New-PSSession to reach server in a cloud (different subnet / tunnel)

Hello!

we work in a big company and have a lot of clouds which are isolated from each other, each cloud is reachable via one jumphost which is reachable and accessible from the local network. Each cloud contains a lot of hosts which are only reachable from the jumphost.

I need to readout or change files etc. from/on the hosts inside the clouds.
I have tried the following to make it work (hostname1 = jumphost, hostname2 = host inside the cloud):

$s = New-PSSession -ComputerName hostname1 -credential ad\username
Import-PSSession -Session $s -CommandName Invoke-Command, New-PSSession -prefix RS

$p = New-RSPSSession -ComputerName hostname2 -credential ad\username
Invoke-RSCommand -session $p -ScriptBlock {Get-ChildItem C:\}
remove-RSpssession $p
remove-pssession $s

It fails with the following error:

Der Parameter "Session" kann nicht gebunden werden. Der Wert "[PSSession]Session1" vom Typ

"Deserialized.System.Management.Automation.Runspaces.PSSession" kann nicht in den Typ

"System.Management.Automation.Runspaces.PSSession" konvertiert werden.

    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeCommandCommand

    + PSComputerName        : hostname1

 

The following works without problems (it's only a readout of the jumphost):


$cred = Get-Credential ad\username -Message "Geben Sie das Passwort fr den Server ein"
$gh = New-PSSession -ComputerName hostname1 -Credential $cred
$sb1 = {Get-ChildItem C:\}
$result = Invoke-Command -Session $gh -ScriptBlock $sb1

I have also tried to nest New-PSSession in invoke-command from here on to reach the host in the cloud, which also fails with an error.

I have tried a lot of things and searched through google to find a solution, but I have to seek your assistance and hope somebody is able to help me.

Thanks a lot!

Regards,

Marc



August 28th, 2015 6:52am

Hi Marc,

well, first you may want to use CredSSP Authentication to avoid any second hop issues.

Then, instead of importing the session, enter it. When transferring objects over the network they get serialized and deserialized (turned into text and back), and session object apparently don't survive the transfer intact. So either you fix the serialization issue (major programming job, and it's not guaranteed that simply implementing serialization will fix it) or you don't transfer the second session over the network to your local computer (thus, you'd enter the first session, or invoke a script on the remote session).

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 7:48am

Hi Fred,

thanks a lot for your quick response!
I will try the CredSSP Authentication, but anyway the enter-psssession cmdlet is working without problems when I enter it manually into the shell:
enter-pssession to open a powershell session to the jumphost --> entering invoke-command with the computer name of the cloud-host manually --> i will get the information I need

But the problem is that I need to get information with the help of a .ps1 script and when I try to use enter-pssession in the script it does not work.

That's why I have tried to get it in another way.
Or do you have a solution to use enter-pssession in a script?

Otherwise I think we have to fix the serialization issue...any ideas here?

Thanks a lot for the help!

regards,

Marc

August 28th, 2015 9:27am

Hi Marc,

fixing it is frankly beyond my skill right now - it's a major c# project though and may well require replacing .NET binaries.

But you do not need to enter the pssession to execute something solely on the remote system. Don't import the session, use Invoke-Command with the Session parameter to run a scriptblock on the remote system.

Example:

$Session = New-PSSession "hostname1"

$script = {
$ses = New-PSSession "OtherRemoteHost"

# Command 1
# Command 2
# ...
# Command n
}
Invoke-Command -Session $Session -ScriptBlock $script

Cheers,
Fred

Ps: You can also create a scriptblock from string, by using [ScriptBlock]::Create($string

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 9:38am

You cannot use Enter-PSSession in a script.  It only works as an interactive session.

 If you use it in a script the subsequent commands are still being executed in the context of the local session.

August 28th, 2015 10:19am

Hey Fred,

this was what I had tried the first time and this was what I mean with "nested" New-Pssession in New-Pssession...it unfortunately does not work, the -session $session2 it shows the error that it contains no data (NULL):

$cred = Get-Credential ad\username -Message "Geben Sie das Passwort fr den hostname 1 ein" $session1 = New-PSSession -ComputerName hostname1 -Credential $cred $cldhost = Read-Host "Geben Sie bitte den hostname2 ein" $cred2 = Get-Credential ad\username -Message "Geben Sie das Passwort fr den Host ein" $sb1 = {$session2 = New-PSSession -ComputerName $cldhost -Credential $cred2} Invoke-Command -Session $session1 -ScriptBlock $sb1 $result = Invoke-Command -Session $session2 -ScriptBlock {Get-ChildItem C:\}

Remove-PSSession $session2, $session1

But maybe I can try like you did to add the command directly behind the New-Pssession command.

Then the second invoke-command in my script is not needed.

Thanks

Marc


Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 11:51am

Hi Marc,

This fails since $Session2 exists only on the remote computer but you are calling it locally.

# Old Line
$result = Invoke-Command -Session $session2 -ScriptBlock {Get-ChildItem C:\}

#New Line
$result = Invoke-Command -Session $session1 -ScriptBlock {Invoke-Command -Session $Session2 -ScriptBlock { Get-ChildItem C:\} }

This might work, but I really do recommend doing it all in one go, as per my example.

Cheers,
Fred

August 28th, 2015 11:55am

I'd recommend creating delegated sessions on that jump host machine, rather than using CredSSP.  That will give you one more "hop" from the jump host to the cloud endpoints without opening you up to the security issues involved in using CredSSP authentication and passing credentials.
Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 12:11pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics