Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from   Instant Windows PowerShell Guide Enhance your knowledge of Windows PowerShell and get to grips with its latest features

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Length 86 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Harshul Patel Harshul Patel
Author Profile Icon Harshul Patel
Harshul Patel
Arrow right icon
View More author details
Toc

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:

  1. 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
    
  2. The following command statement disconnects the session that is stored in the $session variable:

    PS C :\> Disconnect-PSSession -Session $session
    
  3. Finally, the following command statement will reconnect to a session that is stored in the $session variable and will reduce the concurrent connection count to 50:

    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 has PSSession stored in it.

  • -SessionOption <PSSessionOption>: This passes advanced configurations to PSSession by providing the PSSessionOption 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 initiated

    • Drop: If the buffer is full, it overwrites the data; new data takes precedence over the older one

    • None: It takes the value from the property OutputBufferingMode 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 provides AuthenticationMechanism for passed user credentials. The accepted values are Default, Basic, Credssp, Digest, Kerberos, Negotiate, and NegotiateWithImplicitCredential. The parameter holds the default value as Default.

  • -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 the PSCredential object type.

  • -Port <Int32>: This parameter specifies the port number with the accepted integer data type. By default, CMDLET uses the 5985 port for HTTP and 5986 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 are All, Opened, Disconnected, Closed, and Broken. 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 is 32.

    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 passes http://localhost:5985/WSMAN.

  • -AllowRedirection [<SwitchParameter>]: If we use the ConnectionUri parameter and the specified URI for redirecting to some other link, PowerShell won't support this. For that to happen, you need to use the AllowRedirection 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
You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013
Publisher: Packt
ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image