Interactive remoting in CTP3

PowerShell Team

PowerShell Team

Today let’s talk about one of the cool new features of Windows PowerShell V2 – Interactive Remoting.

Let’s start with an example:

PS> $env:COMPUTERNAME # Check local computer name.                                                                      
PS> Enter-PSSession Vladimia06 # Remote to other computer.
[vladimia06]: PS C:\Users\Vladimia\Documents> $env:COMPUTERNAME # Check remote computer name.
[vladimia06]: PS C:\Users\Vladimia\Documents> $PSVersionTable # Check powershell version on remote computer.

Name Value
---- -----
CLRVersion 2.0.50727.3521
BuildVersion 6.1.7015.0
PSVersion 2.0
PSCompatibleVersions {1.0, 2.0}

[vladimia06]: PS C:\Users\Vladimia\Documents> function prompt {} # See how prompt function is always prefixed by [<compu
tername>]: in interactive remoting.
[vladimia06]: PS>Exit-PSSession # Exit remote session.

As you can see Enter-PSSession starts interactive remoting session and Exit-PSSession takes you out of it. The aliases for these commands are etsn and exsn. You can also use exit keyword instead of Exit-PSSession.

Interactive remoting uses the same remoting infrastructure as other PowerShell remoting cmdlets. WinRM provides the transport and maintains a listener service, which is responsible for creation of the remote PowerShell sessions.

Similar to Invoke-Command, you can use computer name to establish remote connection. In that case remote PSSession is created implicitly for you when you enter and disposed of after you exit.

Alternatively, you can create a persistent session using New-PSSession command and use it with Enter-PSSession. In that case the session won’t be closed after you finish and you can enter it multiple times exactly when you need it.

PS> $s = nsn vladimia06  # Create a persistent remote session.                                                          
PS> etsn $s
[vladimia06]: PS C:\Users\Vladimia\Documents> $x = 123 # Create a variable.
[vladimia06]: PS C:\Users\Vladimia\Documents> exsn
PS> etsn $s
[vladimia06]: PS C:\Users\Vladimia\Documents> $x # Check that a variable is still there
[vladimia06]: PS C:\Users\Vladimia\Documents> exsn
PS> icm $s {$x} # The variable created in interactive remoting can also be accessed using remote invocation.
PS> rsn $s # Close the remote session.

Note that the variables that are created in remote session won’t be available in your local runspace when you exit. You need to use Invoke-Command to get them to the client side. The corresponding object will be serialized over the wire and reassembled on the other side. Primitive types will be restored as fully functional ‘live’ objects, but more complex object will be reduced to deserialized ‘property bags’. We will talk more on serialization in one of the next posts.

Try it, use it, love it!
Vladimir Averkin
Windows PowerShell Team


Comments are closed. Login to edit/delete your existing comments