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)

Executing Desired State Configuration (Advanced)

DSC approaches declarative syntax that describes what needs to be done rather than covering imperative syntax that specifies how a task can be performed.

Getting ready

To use DSC, first define a desired configuration. Like functions, configurations in DSC can be defined in the Windows PowerShell language by using the Configuration keyword and stored in script (.ps1) or module (.psm1) files. Also, similar to functions, configurations need to be defined and then run.

How to do it...

  1. To use a configuration, invoke the Configuration block the same way you would invoke a Windows PowerShell function, passing in any expected parameters you have defined (two in the preceding sample). For example, in this case, the MyWebConfig configuration can be invoked as follows:

    PS C :\>MyWebConf -MachineName $env:COMPUTERNAME –WebsitePath \\PSShare\MyWebSites

    This will create a folder with the same name as your configuration name and will contain our MOF output file.

  2. The following command creates an MOF file known as the configuration instance document. Path represents the target directory where your MOF files are located. Wait causes the execution of the DSC resources to run in the background, that is, an interactive process.

    PS C :\>Start-DscConfiguration -Path .\ MyWebConf –Wait –Verbose

How it works...

Each Configuration block must have at least one Node block. Each Node block can have one or more resource provider blocks. You can use the same role provider more than once in the same Node block.

In addition to new language keywords, DSC includes the following set of CMDLETs for managing configurations:

  • Start-DscConfiguration: This CMDLET deploys a configuration to one or more target nodes and applies the configuration on those nodes by using the local configuration manager

  • Get-DscConfiguration: This CMDLET returns the current configuration from one or more target machines:

    PS C :\>$Sess = New-CimSession -ComputerName localhost
    PS C :\>Get-DscConfiguration –CimSession $Sess
  • Restore-DscConfiguration: This CMDLET restores the current configuration from one or more target machines:

    PS C :\>$Sess = New-CimSession -ComputerName localhost
    PS C :\>Restore-DscConfiguration –CimSession $Sess

There's more…

There is one more CMDLET that helps to detect the configuration drift:

  • Test-DscConfiguration: This CMDLET checks for one or more target nodes and returns a Boolean value indicating whether the current desired state matches the actual state. Have a look at the following command:

    PS C :\>$session = New-CimSession -ComputerName localhost
    PS C :\>Test-DscConfiguration –CimSession $session

    This will either return True when the current and actual configuration matches or False if there's a mismatch