PowerShell: Inside Desired State Configuration (DSC) – Get,Set and Test

image_thumb.png

Today I’m going to continue explaining the fundamentals of PowerShell Desired State Configuration (DSC). Yesterdays post focused on running DSC but today, I’m going inside DSC to demonstrate DSC’s indempotance nature and also the Get, Set and Test functions. I’ll do this by walking through the Script DSC Resource.

Indempotent?

When I first heard about PowerShell DSC I was told it was idempotent. I joked well it can happen to anybody(boom-boom!). But then I actually looked up the term and discovered it meant “an operation that will produce the same results if executed once or multiple times”

In fact, if you run a DSC script it will TEST the configuration first. If the configuration evaluates as non compliant then it will SET the configuration. Running the script/configuration/resource again will do nothing further as the configuration will now evaluate as compliant.

Inside the DSC Resources.

This is a DSC Resource I wrote called STMS_cAuthoriseDhcpServer. It has 3 functions that are common in all DSC Resources. Get-TargetResourse, Set-TargetResourse and Test-TargetResourse.

image

These functions must all exist and have the names given above.

Example Configuration.

OK let’s look at todays script.

Configuration HelloWorld {
    Script Demo
    {
        GetScript = {
            $File = 'C:\ScriptimusExMachina\DSCHelloWorld.txt'
            $Content = 'Hello World!'
            $Results = @{}
            $Results['FileExists'] = Test-path $File
            $Results['ContentMatches'] = Select-String -Path $File -SimpleMatch $Content -Quiet

            $Results
        }
        SetScript = {
            'Hello World!' | Out-File C:\ScriptimusExMachina\DSCHelloWorld.txt
        }
        TestScript = {
            $File = 'C:\ScriptimusExMachina\DSCHelloWorld.txt'
            $Content = 'Hello World!'

            If ((Test-path $File) -and (Select-String -Path $File -SimpleMatch $Content -Quiet)) {
                Write-Verbose 'Both File and Content Match'
                $True
            }
            Else {
                Write-Verbose 'Either File and/or content do not match'
                $False
            }

        }
    }
}

It’s another Hello World DSC configuration that does exactly the same as yesterdays script, but this time, I’m using a built in resource called Script.

Just as I did yesterday, I paste the configuration into the console(for testing) and execute it to create my MOF file.

image_thumb5

Just as before, launching it creates a text file with the contents “Hello World!”

image_thumb7

The Script Resource

We can explore a DSC resource by using the Get-DSCResource cmdlet. The syntax parameter shows the actual code that we can use in our configuration. It is a common practice to just copy and paste the syntax into a code editor.

image

This resource will hold 3 script blocks. The Get, Set and Test code.

Get.

This code block returns a hashtable and is run only when you use the Get-DSCConfiguration cmdlet to view the values of the current state. It does not affect the execution of DSC in push or pull modes.

GetScript = {
    $File = 'C:\ScriptimusExMachina\DSCHelloWorld.txt'
    $Content = 'Hello World!'
    $Results = @{}
    $Results['FileExists'] = Test-path $File
    $Results['ContentMatches'] = Select-String -Path $File -SimpleMatch $Content -Quiet

    $Results
}

 Set.

This code block applies the configuration when DSC is executed. It contains the code required to return the configuration to the desired state. Within this section you can call functions or any available cmdlets needed.

SetScript = {
    'Hello World!' | Out-File C:\ScriptimusExMachina\DSCHelloWorld.txt
}

Test.

This code block evaluates the configuration and returns a Boolean value of either True or False. It is executed when either Start-DSCConfiguration Test-DSCConfiguration are launched. During a DSC configuration process, this function is run first and if it evaluates as true then the Set is launched immediately.

TestScript = {
    $File = 'C:\ScriptimusExMachina\DSCHelloWorld.txt'
    $Content = 'Hello World!'

    If ((Test-path $File) -and (Select-String -Path $File -SimpleMatch $Content -Quiet)) {
        Write-Verbose 'Both File and Content Match'
        $True
    }
    Else {
        Write-Verbose 'Either File and/or content do not match'
        $False
    }

}

Summary

The power of DSC is in these 3 little functions. They represent good standards that could be applied to any kind of infrastructure scripts.

Advertisements

About Andrew Barnes

A Scripting and Deployment Specialist.
This entry was posted in Deployment, Desired State Configuration, DSC, PowerShell, Scripting and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s