Book Image

Instant Windows PowerShell Guide

By : Harshul Patel
Book Image

Instant Windows PowerShell Guide

By: Harshul Patel

Overview of this book

Windows PowerShell has become a booming scripting language over the last couple of years. It has extensive support with an ample number of vendor products, providing a standardized platform for automation and administration. It has massive support for all Microsoft products which creates a layer that can easily automate everything. In the latest version, the PowerShell team has introduced much more functionality with thousands of CMDLETs, part of various modules.This book is a quick reference guide to enable you to get the most out of the latest Windows PowerShell techniques. In this book, you will find new enhancements in the latest version of PowerShell with some helpful examples. This book enables you to quickly move from older versions of PowerShell to Version 3.0 and Version 4.0.This practical, example-oriented book helps you to overcome the difficulty of using and discovering CMDLETs by providing precise information about everything that has been newly introduced in the latest version of Windows PowerShell. It also focuses on the new configuration management system with the help of DSC as a new feature of Windows PowerShell v4.0.You will learn how to use the newly introduced CMDLETs and parameters to perform daily routine tasks. You will also learn how to administer the servers remotely and maintain persistent sessions to provide continuity. You will gain an insight into writing efficient scripts by using various parameters, snippets, and workflows to gain more productivity. You will also be introduced to various modules like CimCmdlets, PSScheduledJob, PSDesiredStateConfiguration, and so on in order to enhance your scripts with the latest instrumentation. Finally this book will make you aware of the capabilities of PowerShell v4.0 and how to fully leverage the functionality introduced in the new version.
Table of Contents (7 chapters)

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.


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.


  • -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.


    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.


    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.


    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.


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.


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