Session scheme (Intermediate)
There are numerous changes in PowerShell Version 3.0 with respect to PowerShell remote sessions. Previously, in Version 2.0, all remote PSSessions
were dependent on the current console session. Now, in Version 3.0, PowerShell maintains the remote PSSession
on the remote computer itself, and it is totally independent on the current console session. So, even though you close the current local session, you can continue and resume the session, and reconnect them.
Getting ready
There are a few recently introduced parameters for Get-PSSession
and New-PSSession
CMDLETs. Also, there are some newly introduced CMDLETs such as Connect-PSSession
, Disconnect-PSSession
, and New-PSTransportOption
.
How to do it...
Try executing the following lines of code by performing the following steps:
The following command statement creates a new remote PowerShell session with a count of 100 concurrent connections to computers mentioned in
servers.txt
using the PSDomain/PSAdmin privilege, and it is stored in a variable named$session
.PS C :\> $session = New-PSSession -ComputerName (Get-Content C:\servers.txt) -Credential PSDomain\PSAdmin -ThrottleLimit 100
The following command statement disconnects the session that is stored in the
$session
variable:PS C :\> Disconnect-PSSession -Session $session
Finally, the following command statement will reconnect to a session that is stored in the
$session
variable and will reduce the concurrent connection count to50
:PS C :\> Connect-PSSession -Session $session -ThrottleLimit 50
How it works...
As discussed earlier in this chapter, with PowerShell Version 3.0, we have the facility to reconnect to the remote session that is disconnected due to certain reasons such as network interruption, remote server unavailability, and so on. In such cases, we can use a couple of commands to handle the sessions; they are Connect-PSSession
and Disconnect-PSSession
.
The Connect-PSSession
CMDLET connects to the disconnected session again and enables us to resume our work in the same session. It has certain parameters such as Authentication
, CertificateThumbPrint
, ComputerName
, Credential
, Id
, Name
, Port
, ThrottleLimit
, UseSSL
, AllowRedirection
, ConnectionUri
, and so on, which have been discussed earlier in this book. The following are two parameters specific to sessions:
-Session <PSSession[]>
: This parameter accepts a value from a variable that hasPSSession
stored in it.-SessionOption <PSSessionOption>
: This passes advanced configurations toPSSession
by providing thePSSessionOption
object to it. This parameter takes default values from the$PSSessionOption
preference variable.
Note
We can create the PSSessionOption
object by using the New-PSSessionOption
CMDLET.
The Disconnect-PSSession
CMDLET disconnects the specified session from the current session. You can provide a reference to the session by having parameters such as Name
, Session
, Id
, and so on. There are a few other parameters that are specified as follows:
-IdleTimeoutSec <Int32>
: This parameter confirms for how long would a disconnected session be maintained at the remote computer's end. By default, the value is set to 7,200,000 milliseconds (two hours). The minimum and maximum values are 60 sec (one min) and 12 hours respectively.-OutputBufferingMode <OutputBufferingMode>
: This parameter specifies how the output is stored in the buffer. The accepted values and actions are as follows:Block
: If the buffer is full, the execution will be suspended until the buffer cleaning process is initiatedDrop
: If the buffer is full, it overwrites the data; new data takes precedence over the older oneNone
: It takes the value from the propertyOutputBufferingMode
that is provided with the session configuration for a disconnected session
There's more…
A few parameters are also introduced with respect to PowerShell sessions in Version 3.0. The following are some of these parameters.
Get-PSSession
-Authentication <AuthenticationMechanism>
: This parameter explicitly providesAuthenticationMechanism
for passed user credentials. The accepted values areDefault
,Basic
,Credssp
,Digest
,Kerberos
,Negotiate
, andNegotiateWithImplicitCredential
. The parameter holds the default value asDefault
.-CertificateThumbprint <String>
: This parameter's role is to supply the digital public key certificate of a user who has permission to create a session over the network. It creates a temporary connection using certificate-based authentication.Tip
To get a certificate thumbprint, you can explore the
Cert: PS
drive.-Credential <PSCredential>
: This parameter provides an explicit credential that has permission to the console for getting session information from the remote computer. It accepts thePSCredential
object type.-Port <Int32>
: This parameter specifies the port number with the accepted integer data type. By default, CMDLET uses the5985
port for HTTP and5986
for HTTPS communications.Note
Port numbers specified in the
Port
parameter apply to all the computers and sessions in the command statement.-State <SessionFilterState>
: This parameter is to retrieve the session information of the specific session state. The accepted values for a session state areAll
,Opened
,Disconnected
,Closed
, andBroken
. By default, CMDLET retrieves information for all the sessions.-ThrottleLimit <Int32>
: This parameter provides the maximum concurrent connection count for the specified computers. By default, the maximum concurrent connection count is32
.Tip
This value of the
Throttlelimit
parameter does not apply to sessions; it is limited to the command statement in which it is provided.-UseSSL [<SwitchParameter>]
: Using this parameter, we can create a connection using the Secure Socket Layer (SSL) protocol. By default, SSL does not enable to create connections.-ConnectionUri <Uri[]>
: This parameter is to explicitly provide a Uniform Resource Identifier that defines the connection configuration options. The syntax is as follows:<Transport>://<ComputerName>:<Port>/<ApplicationName>
. By default, it passeshttp://localhost:5985/WSMAN
.-AllowRedirection [<SwitchParameter>]
: If we use theConnectionUri
parameter and the specified URI for redirecting to some other link, PowerShell won't support this. For that to happen, you need to use theAllowRedirection
parameter which allows redirecting from a specified URI.
Tip
In PowerShell Version 3.0, you can import a module from a remote computer to a local computer using PowerShell remote sessions with the use of the Import-Module
parameter. It loads and unloads the specified module based on session availability.
New-PSTransportOption
We have one more CMDLET named New-PSTransportOption
that can be leveraged to configure the advanced session configuration options. This CMDLET has properties such as IdleTimeoutSec
, MaxConcurrentCommandsPerSession
, MaxConcurrentUsers
, MaxIdleTimeoutSec
, MaxMemoryPerSessionMB
, MaxProcessesPerSession
, MaxSessions
, MaxSessiconsPerUser
, OutputBufferingMode
, and ProcessIdleTimeoutSec
.
There are some other session configurations CMDLETs also introduced with the release of PowerShell Version 3.0. We can check for more information using the following help topics:
PS C :\> help about_Session_Configurations PS C :\> help about_Session_Configuration_Files