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)

Working remotely (Advanced)

There have been multiple modifications to PSRemoting with the release of Windows PowerShell Version 3.0. To perform remoting activities with PowerShell, we need to execute the Enable-PSRemoting CMDLET on remote computers.

Getting ready

  • Enable-PSRemoting: This CMDLET in fact starts the WinRM service, sets it to the type automatic, creates a firewall exception, and prepares a session environment to perform remote tasks.

  • -SkipNetworkProfileCheck [<SwitchParameter>]: Server versions of Windows have remote access from the local subnet by default, but if we are working with a client version and the computer is in the public network, we won't have remote access from the local subnet. In such cases, we need to either use the SkipNetworkProfileCheck parameter, or we need to create a firewall rule manually by using the Set-NetFilewallRule CMDLET of the NetSecurity default module.

  • Disable-PSRemoting: This CMDLET only prevents remote access to the computers. You need to manually stop and disable the WinRM service and also need to disable firewall exceptions for remote communications.


By default, Windows Server 2012 is enabled with PSRemoting, but for lower versions of operating systems, use Enable-PSRemoting.

How to do it...

Execute the following command statement, by performing the step:

The following command statement executes DiskInventory.ps1 scripts on computers specified in servers.txt, and it maintains the disconnected session named DiskInventory within the scope of the current console:

PS C:\>Invoke-Command -ComputerName (Get-Content C:\servers.txt) –SessionName DiskInventory –InDisconnectedSession -FilePath \\Scripts\DiskInventory.ps1 -NoNewScope

How it works...

There are a few new parameters introduced with the Invoke-Command CMDLET.

  • -EnableNetworkAccess [<SwitchParameter>]: This parameter supplies a security token to loopback sessions. This token allows you to run commands in a loopback session from a local computer and get data from remote computers. This parameter only works with loopback sessions.


    A loopback session is a PowerShell session that is created on the local computer. To create a loopback session, use the –ComputerName parameter with the . or localhost value.

  • -InDisconnectedSession [<SwitchParameter>]: This parameter facilitates us to run a command statement or a script in the disconnected session. With this parameter, Invoke-Command creates a new PSSession on remote computers and starts the execution of ScriptBlock or a script specified with the FilePath parameter. Then, it disconnects the session, and the execution happens in the disconnected session in the background.

  • -NoNewScope [<SwitchParameter>]: By default, Invoke-Command is executed in the command's scope. Using this parameter, we make Invoke-Command execute in the current console's scope instead of the command's own scope.

  • -SessionName <String[]>: This parameter is only applicable while using the InDisconnectedSession parameter. We can explicitly provide a name to the disconnected session by using this parameter.

There's more…

You can learn to use a local variable in the remote PowerShell session by going through the following explanation.

Remoting local variable via $Using

We can leverage local variables into remote sessions by the Using keyword with variable names that are introduced with Windows PowerShell v3.0. Refer to the following example:

PS C:\> $PSCred = Get-Credential PSDomain\PSAdmin
PS C:\> Invoke-Command -ComputerName DC01,Member01,Member02 -ScriptBlock {Restart-Computer -Credential $Using:PSCred}

The preceding command statements create the PSCred local variable containing the PSDomain\PSAdmin credential along with Password. The second line of the command statement utilizes the PSCred variable and the Using keyword in remote sessions to reboot machines.