Getting started with Desired State Configuration – Part 2

In the first part we got DSC up and running with some custom modules. In this part we will start using some of the built in modules. We start by creating a Configuration. In this case we want to create a configuration for a DC which means that we will need a DHCP Server. We start by installing the DHCP Role:

configuration DC
{
    
    # One can evaluate expressions to get the node list
    # E.g: $AllNodes.Where("Role -eq Web").NodeName
    node ("DC01")
    {
        # Call Resource Provider
        # E.g: WindowsFeature, File
        WindowsFeature DHCPServer
        {
           Ensure = "Present"
           Name = "DHCP"
        }
    }
}

DC

Start-DscConfiguration -Path .\DC

When the configuration is created we run it to create the .mof (the Mof file is created in a folder with the same name as the node in the folder where you are running the configuration block) file for the configuration and then we start the configuration by invoking Start-DscConfiguration.

We continue to build our configuration by using the Service Dsc module. By running:

Get-DscResource Service | select -ExpandProperty Properties

we will get more information about the module and which keywords we can use. We use this to set the State and StartupType

configuration DC
{
    
    # One can evaluate expressions to get the node list
    # E.g: $AllNodes.Where("Role -eq Web").NodeName
    node ("DC01")
    {
        # Call Resource Provider
        # E.g: WindowsFeature, File
        WindowsFeature DHCPServer
        {
           Ensure = "Present"
           Name = "DHCP"
        }

        Service DHCP
        {
            State = "Running"
            Name = "DHCPServer"
            StartupType = "Automatic"
        }
    
    }
}

DC

Start-DscConfiguration -Path .\DC

To verify that the configuration is applied  and that it has not been changed we use:

Compare-DscConfiguration -Path .\DC

If everything is OK we will see this:

PSComputerName  ResourcesInDesiredState        ResourcesNotInDesiredState     InDesiredState 
--------------  -----------------------        --------------------------     -------------- 
DC01            {[WindowsFeature]DHCPServer... {}                             True           

Try stopping the DHCP Service and run the same command again

 

PSComputerName  ResourcesInDesiredState        ResourcesNotInDesiredState     InDesiredState 
--------------  -----------------------        --------------------------     -------------- 
DC01            [WindowsFeature]DHCPServer     [Service]DHCP                  False          

As you can see we can now find configuration drift… in the next part we will look at how to enforce the configuration.

/Johan

Comments

Leave a Reply