Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from  Instant Windows PowerShell Guide

Product type Book
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Pages 86 pages
Edition 1st Edition
Languages
Author (1):
Harshul Patel Harshul Patel
Profile icon Harshul Patel
Toc

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.

arrow left Previous Section
You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013 Publisher: Packt ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}