Powershell Desired State Configuration simple example

While still in ‘Devop Mood’, let’s quickly figure out Desired State Configuration.

This image gives a nice overview of the DSC architecture:

Requirements

If you have a Windows 2012 R2 Server with the latest updates and KB2883200 installed, you’re good to go. Check it like so:
wmic qfe | find "KB2883200"

What Powershell Modules are there installed anyway? Go ahead, open a Powershell console and type $env:PSModulePath -split ";"
This displays the locations on your PC where there are Powershell modules installed.

PS C:\Users\vagrant> $env:PSModulePath -split ";"
C:\Users\vagrant\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

Now if you cd into C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ you get to see all the modules. If all is well, one of them is PSDesiredStateConfiguration. This is where the DSC commandlets are hidden.

Resources

A resource is an ‘object’ which you can configure with DSC. There are 3 sources for resources.

Out of the box resources
Out of the box with Powershell v4, there are modules for File, Registry, Services etcetera. To see the complete list, do:

PS C:\> Get-DscResource | select name

This outputs:

Name
----
File
Archive
Environment
Group
Log
Package
Registry
Script
Service
User
WindowsFeature
WindowsProcess

Community resources
The Powershell community has also written some modules for DSC resources, like DNS, Active Directory and Hyper-V. You can find them here and they are prefixed with a c.

Experimental resources
The Powershell team itself also provides some experimental resources which you can find here. These resources are prefixed with an x.

You can download these resources and add them to the $env.PSModulePath folder. Which is in my case: C:\Windows\system32\WindowsPowerShell\v1.0\Modules\.

Create your first configuration

These steps describe the DSC process:

  • Add the required DSC resources
  • Create a configuration script
  • Execute the script to generate a MOF file
  • Apply the MOF to the target nodes

Now let’s start with adding a folder named “c:\Replica” on every node. That means we could use the File resource, which comes out of the box. Now, how to use this file resource? Luckily, the Powershell folks have made things very easy for us. We can just type get-dscresource File -syntax and lo and behold:

PS C:\> Get-DscResource File -Syntax
File [string] #ResourceName
{
    DestinationPath = [string]
    [ Attributes = [string[]] { Archive | Hidden | ReadOnly | System }  ]
    [ Checksum = [string] { CreatedDate | ModifiedDate | SHA-1 | SHA-256 | SHA-512 }  ]
    [ Contents = [string] ]
    [ Credential = [PSCredential] ]
    [ DependsOn = [string[]] ]
    [ Ensure = [string] { Absent | Present }  ]
    [ Force = [bool] ]
    [ MatchSource = [bool] ]
    [ Recurse = [bool] ]
    [ SourcePath = [string] ]
    [ Type = [string] { Directory | File }  ]
}

So this basically explains how to use the File resource.

Fire up Powershell ISE and start type:

Configuration MyFirstConfig {

Node DSC1 {
    
    File SyncDir {
        DestinationPath = "c:\replica"
        Type = "Directory" 
        Ensure = "Present"   
    }
  }
}

MyFirstConfig -OutputPath c:\DSC

If you execute this script a MOF file will be created in c:\DSC.

Apply the MOF

Now go ahead and type: Start-DscConfiguration -Path c:\DSC

This will output:

PS C:\Windows\System32\WindowsPowerShell\v1.0\Modules> Start-DscConfiguration -Path c:\DSC

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
8      Job8            Configuratio... Running       True            DSC1                 Start-DscConfiguration...

And if all is well there is indeed a folder named ‘replica’ on your C:\ drive.

Conclusion

This was a very simple example of Powershell DSC. I think it is quite nice and I am planning to explore its possibilities in the near future (like tomorrow or so).

 
 
 

2 Replies to “Powershell Desired State Configuration simple example”

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: