Monday, March 23, 2015

PowerShell : A deep dive into remoting - Part 5


PowerShell sessions are the core components of PowerShell remoting. So let’s spend some more time understanding the details of PowerShell sessions and the configurations. Every time we execute the Invoke-Command with the -ComputerName argument, a temporary session is created to execute the remote command and then released after the command is executed. A PowerShell session or PSSession represents a connection between the computer that invokes a remote command and the remote computer. A good practice to follow when you are planning to connect to a remote computer several times and executing commands is to create a PSSession and reuse this session for the work. This way, you can manage a single connection to a PowerShell host instance and have a persisted session in use.
When using a persistent connection, the commands that are executed against the session will remain in effect till the session is removed. For e.g. You can add snapins and load modules at the beginning and then use the cmdlets or functions from these snapins/ modules later in the session.

Creating and using sessions
To create a persistent session, you can use the New-PSSession command as given below
$session = New-PSSession -ComputerName COMP1

The command creates a session object and assigns it the the $session variable. The New-PSSession command also allows -Credential parameter to use to connect to the remote computer as a specific user.
$session = New-PSSession -ComputerName COMP1 -Credential (Get-Credential)

By default PowerShell uses the port 5985 for HTTP and 5986 for HTTPS connections. If the remote computer has the WinRM configured on a different port, you can mention that Port using the -Port argument for the New-PSSession command.
Every time a session is created PowerShell automatically uses the default session configuration unless you specify a different session configuration or have secured session configurations. If you want to override this behavior then you need to create a Session configuration and use the –ConfigurationName argument to use this configuration for the session.

After the session is created you can initiate a one-one connection to the remote computer using an Enter-PSSession cmdlet by using the –Session argument
Enter-PSSession –Session $session

The session object that is created will remain open till it is closed explicitly. You can use the Get-PSSession cmdlet to check the currently available sessions that is created.
Get-PSSession | fl *

Disconnect and connect to sessions
If you want to execute a long running process like configuring a SharePoint farm on a remote computer by using a remote session and you don't want to connect to the session from the current computer, but later connect from a different machine, you can use the Disconnet-PSSession and Connect-PSSession cmdlets.

The Disconnect-PSSession cmdlet disconnects a PSSession, from the current session. As a result, the PSSession is in a disconnected state. You can connect to the disconnected PSSession from the current session or from another session on the local computer or a different computer. Note that Disconnect-PSSession cmdlet disconnects only open PSSessions that are connected to the current session. Disconnect-PSSession cannot disconnect broken or closed PSSessions, or interactive PSSessions started by using the Enter-PSSession cmdlet, and it cannot disconnect PSSessions that are connected to other sessions.
You have to explicitly perform the action of disconnecting the session. A good practice is to start a session from one computer, disconnect it, and then reconnect to that session from another computer.

Get-PSSession -Session $session | Disconnect-PSSession
Once you have disconnected the session from the current host, you can later use the Connect-PSSession to reconnect the session from another computer by specifying the ComputerName that the session is available.

Connect-PSSession -ComputerName COMP1
Using the session options
There are a number of other cmdlets for the advanced configuration of remote PowerShell sessions. The first and most obvious of these is the New-PSSessionOption cmdlet, which enables an administrator to configure advanced options for use with the New-PSSession cmdlet. For instance, if you want to change the default culture to nl-nl and set the maximum object size that can be recived to 10 MB, you can use the

$nlSession = New-PSSessionOption -Culture "nl-nl" -MaximumReceivedObjectSize 10MB
To persist a session option created to use later, you can export the object to xml using the Export-CliXml cmdlet and then later import the object when creating a new session.

$nlSession | Export-CliXml c:\PSSessions\nlSession.xml
New-PSSession -ComputerName COMP1 -SessionOption (IMport-CliXml c:\PSSessions\nlSession.xml)