PowerShell DSC 101 Part 2: Configuration

Your Configuration is exactly that: the configuration of the nodes you specified in your ConfigurationData.

From Part 1:

Configuration – This is the collection of information that tells PowerShell DSC how nodes specified in the ConfigurationData should be configured. I.e. which windows features should be installed, How File Shares should be configured, How IIS should be set and so on.

Let’s take a look at a very basic Configuration:

Configuration XPSRemover #Keyword + name of the keyword
{

	Import-DscResource -ModuleName PSDesiredStateConfiguration #Here we are importing a special resource used by DSC to enact the configuration

    Node localhost #Here we are telling DSC that we want to run this on the localhost, i.e. the machine we run the configuration on
	{
		WindowsFeature XPS-Viewer-Removal #Here we say what we want to configure (a windows feature) and give that configuration block a name.
		{
			Name = "XPS-Viewer" #Here we declare the name of the feature we want to remove.
			Ensure = "Absent" #Here we declare that we want the feature to be uninstalled if it is currently installed
		}
	}
}

Let us unpack this line by line:

Configuration XPSRemover #Keyword + name of the keyword

When you create a function, you use the keyword ‘Function’ in the format:

Function Import-MyFancyFunction

With the Configuration keyword, you can now call the configuration from a PowerShell prompt like you would a function you wrote.

Import-DscResource -ModuleName PSDesiredStateConfiguration #Here we are importing a special resource used by DSC to enact the configuration

The Import-DSCResource is a special command that you only use within a Configuration, you can’t just use it from a PowerShell prompt.
PSDesiredStateConfiguration” is a special resource as mentioned in Part 1. This allows PowerShell DSC to configure settings that the resource knows about. You will find

This is similar in function to how you need to “Import-Module ActiveDirectory” in order to use any of the AD PowerShell CMDlets.

Node localhost #Here we are telling DSC that we want to run this on the localhost, i.e. the machine we run the configuration on

This tells PowerShell DSC that we want to run this configuration on the local host. You could have an actual hostname here, and later we’ll look at how to use roles.

WindowsFeature XPS-Viewer-Removal #Here we say what we want to configure (a windows feature) and give that configuration block a name.

Here we tell PowerShell DSC that we want to configure a WindowsFeature. The name we give this block should tell us, the human reading the code, what we want to do, rather than tell PowerShell anything.

Name = "XPS-Viewer" #Here we declare the name of the feature we want to remove.

Here we need the exact name of the windows feature we want to install/remove. This needs to match what you see when you run Get-WindowsFeature at a PowerShell prompt.

Ensure = "Absent" #Here we declare that we want the feature to be uninstalled if it is currently installed

If ensure is set to ‘Present’, then the feature will be installed if it is missing, if ‘Absent’ is declared, then the feature will be removed if found.

So, that’s a very very basic Configuration. Running this in PowerShell will not make any changes to your system, but will instead create a MOF (Management Object Format) file as can be seen in this screenshot:

dsc-mof

One thought on “PowerShell DSC 101 Part 2: Configuration

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