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)

Understanding Desired State Configuration (Advanced)

With the release of Windows PowerShell v4.0, one new feature is introduced: Desired State Configuration. In fact, it is available as a feature in Windows Server 2012 R2 Preview OS. Windows PowerShell Desired State Configuration is a set of extensions and providers that enable to declare, repeatedly deploy, and configure data center resources. DSC enables us to define the configuration of the target nodes (computers or devices) and prevent configuration inconsistencies.

Getting ready

We are living in a world where device adoption is quicker than ever. Hence, we must ensure that all the devices are in a desired state. DSC allows us to manage all the devices using Windows PowerShell.

How to do it...

The DSC flow can be categorized into two different models: Push and Pull. The DSC process could be divided in to three phases as follows:

  • The Authoring phase: In this phase, we need to declare the DSC configuration. The outcome of this phase would be of one or more Management Object Format (MOF) files, a format that is consumable by DSC. MOF files can be created in multiple ways; the simpler one is that of using Windows PowerShell ISE. Using PowerShell v4, we can add declarative syntax extensions and IntelliSense for making it easier to create an MOF file. It handles schema validations as well.

  • The Staging phase: In this phase, actions are carried out based on the model that we are using either Push or Pull. In case of adopting the Pull Model, DSC data and custom providers are kept on the Pull server. A Pull server is an IIS web server with a well-defined OData interface. The target system contacts the Pull Server by passing a URI and a unique identifier to retrieve its configuration and verifies whether all the custom providers are available. If not, they are downloaded to the target system. If we're using the Push Model, DSC data is being pushed to the target system.

    One catch though is that you need to make sure your custom provider exists on the target system. You need to place them at "%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSProviders".

  • The "Make it so" phase: The final phase is to apply the configuration to "make it so". The DSC data is either pulled or pushed to the Local Configuration Store and contains the current, previous, and the desired state configuration (DSC). The configuration then gets parsed and the relevant provider (WMI) implements the change and "makes it so".

How to define the Configuration block

The following is a sample configuration block to declare configuration for one or more nodes under the name MyWebConf:

configuration MyWebConf
{  # Parameters are optional
   param ($MachineName, $WebsitePath)
   # A Configuration block can have one or more Node blocks
   node $MachineName
   {  # Next, specify one or more resource provider blocks
      # WindowsFeature is one of the providers you can use in a Node block
      # This example ensures the Web Server (IIS) role is installed
      WindowsFeature IIS
      {   Name  = "Web-Server" # Use the Name property from Get-WindowsFeature
          Ensure= "Present" # To uninstall the role, set Ensure to "Absent" }
      # You can use the File provider to create files and folders
      # "File" is the name of the resource provider to use
      # "WebDirectory" is the name you want to use to refer to this instance
      File WebDirectory
      {  SourcePath      = $WebsiteFilePath
         DestinationPath = "C:\inetpub\wwwroot"
         Requires     = "[WindowsFeature] IIS" # Use Requires for dependencies
         Ensure       = "Present" # You can also set Ensure to "Absent" }

How it works...

The DSC feature consists of multiple subcomponents. The information is as follows:

  • Local Configuration Manager (LCM): This is a part of the DSC system that implements configuration data on the target nodes.

  • Windows PowerShell language extensions: DSC extends the Windows PowerShell language to support importing the MOF schema files that are converted to Windows PowerShell keywords. The keywords are used to describe the desired configuration of resources in the data center. The following are the two keywords:

    • Configuration: This defines the desired configuration

    • Node: This defines the desired configuration for one or more nodes

  • File share pull provider: The file share pull provider is an auxiliary module that is used by LCM to retrieve the configuration of MOF files and providers from a file share or a local folder.