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)

Exploring various configuration providers (Advanced)


A collection of configuration providers, which are known as resources, is a part of the core DSC system. These providers enable you to configure roles and features, copy files, create a registry entry, manage services, create local users and groups, and so on.

Getting ready

Each resource is technically represented by a DSC provider. The default location for these DSC providers is at C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSProviders.

How to do it...

There are various DSC providers available. The following is the syntax for a few of them:

  1. The following example uses the Environment resource to define or confirm the value of the specified environment variable:

    Environment MyEnv
            {   Ensure ="Present" # You can also set Ensure to "Absent"
                Name   ="MyEnvVariable"
                Value  ="MyValue" }
  2. The following example installs or verifies the installation of the specified feature:

    WindowsFeature MyFeature
            {   Ensure  = "Present"
                Name    = "MyFeatureName" }

You can get the list of DSC providers by using following command:

PS C:\> Get-DscResource

How it works...

As stated earlier, there are multiple resources available. I've provided information on some of these resources here.

Archive resources

The Archive resource unpacks archive (.zip) files at the given path.

Archive MyArchive
{  Ensure      ="Present" # You can also set Ensure to "Absent"
   Path        ="C:\PS\MyScripts.zip"
   Destination ="C:\PS\MyScripts" }

Group resources

The Group resource manages local groups on the target machine.

Group MyGroup
{     Ensure   ="Absent" # This will remove MyGroup, if present
      GroupName="MyGroup" }

Package resources

The Package resource installs and manages packages such as MSIs on the target machine.

Package MyPackage
{  Ensure ="Present"# You can also set Ensure to "Absent"
   Path   ="$FilePath\MySoftware.msi"
   Name   ="MyPackage" }

Registry resources

The Registry resource manages registry keys and values.

Registry MyRegistry
{  Ensure    ="Present" # You can also set Ensure to "Absent"
   Key       ="HKEY_LOCAL_MACHINE\SYSTEM\MyHiveKey"
   ValueName ="RegName"
   ValueData ="RegData" }

Script resources

The Script resource defines ScriptBlock that runs on target nodes. The TestScript block runs first. If it returns False, the SetScript block starts running.

Script MyScript
{
   SetScript  = { # This block will run if TestScript returns False }
   TestScript = { # This block runs first }
   GetScript  = { # This must return a hash table }
}

Service resources

The Service resource manages services on the target machine.

Service MyService
{  Name       ="MyService"
   StartupType="Automatic" }

User resources

The User resource manages local user accounts on the target machine.

User MyUser
{  Ensure   ="Present" # To delete a user account, set Ensure to "Absent"
   UserName ="MyName"
   Password =$MyPassword # This needs to be a credential object
   Requires ="[Group]MyGroup"# Configures MyGroupfirst }

There's more…

We can integrate any solution with DSC, and the minimal requirement is that you should be able to run the PowerShell script in such environments. You can create your own custom resources; we'll discuss this now.

Requirements for creating a custom DSC resource

To implement a DSC custom resource, create a new folder directly under \Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSProviders. Rename the folder as your custom resource, and you need to declare the following three files into it:

  • MOF schema: The MOF schema defines the properties of the resource. To use your custom resource in a DSC configuration script, you assign values to these properties to indicate the configuration options. Then, save the MOF schema to a file called CustomResourceName.schema.mof.

  • Script module: This defines the logical aspect of your resource. It consists of three functions: Get-TargetResource, Set-TargetResource, and Test-TargetResource. These functions take parameter sets as per the definition of the MOF schema file. Declare these three functions in a file called CustomResourceName.psm1. The Get-TargetResource function returns a hash table that lists all the resource properties as keys and the actual values of these properties as the corresponding values. Depending on the values that are specified for the resource properties in the configuration script, Set-TargetResource must perform appropriate actions. Finally, Test-TargetResource matches the status of the resource instance that is specified in the key parameters. It shows the Boolean output as either True or False based on the matching of key parameters.

  • Module manifest: Finally, use the New-ModuleManifest CMDLET to declare a CustomResourceName.psd1 file for your new custom resource. Define Get-TargetResource, Set-TargetResource, and Test-TargetResource as a list of functions.