Using A Single DSC Configuration for Multiple Servers

Ok so we’ve showed you some cool things. Building a DSC file, applying that configuration to a remote DMZ server.

But one thing you’ll probably have noticed. EACH and EVERYTIME I have done this I have typed in the name of the server in the Configuration. Seems a bit silly doesn’t it?

So unless I want to have a Configuration for each one of my servers. That doesn’t make too much sense. For example, if I want to spin up a file server, I’m pretty certain each of those servers will have the same features.

So under my present setup for two File servers called CONTOSO-FPS1 and CONTOSO-FPS2 I would have done something like this.

Configuration MySampleFileServer

{

    Node CONTOSO-FPS1

    {

        WindowsFeature FileStorage

        {

        Ensure = "Present"

        Name = "FileAndStorage-Services"

        }

        WindowsFeature PrinterSharing

        {

        Ensure = "Present"

        Name = "Print-Services"

        }

    }

    Node CONTOSO-FPS2

    {

        WindowsFeature FileStorage

        {

        Ensure = "Present"

        Name = "FileAndStorage-Services"

        }

        WindowsFeature PrinterSharing

        {

        Ensure = "Present"

        Name = "Print-Services"

        }

    }

}

 

This would absolutely work but I see a problem. If we CHANGE our standard File server configuration, we’ll have to edit EACH and EVERY Node. That’s not only time consuming but prone to errors.

 

However DSC works very much like any standard PowerShell function does, including the ability to pass parameters.

 

So what we CAN do is simply is inject a parameter at the top and replace the node name with a variable. So our ORIGINAL DSC would go from looking like this.

 

Configuration MySampleFileServer

{

    Node CONTOSO-FPS

    {

        WindowsFeature FileStorage

        {

        Ensure = "Present"

        Name = "FileAndStorage-Services"

        }

        WindowsFeature PrinterSharing

        {

        Ensure = "Present"

        Name = "Print-Services"

        }

    }

}

 

To this by adding a Param() statement and giving our Node a PowerShell variable instead.

 

Configuration MySampleFileServer

{

param($Nodename)

 

    Node $Nodename

    {

        WindowsFeature FileStorage

        {

        Ensure = "Present"

        Name = "FileAndStorage-Services"

        }

        WindowsFeature PrinterSharing

        {

        Ensure = "Present"

        Name = "Print-Services"

        }

    }

}

 

With this updated DSC we can specify the Name of the node like so

 

MySampleFileServer –nodename ‘CONTOSO-FPS1’,’CONTOSO-FPS2’,’FABRIKAM-FPS7’

 

Or you could have a list of servers in a CSV file and even do something like this

 

IMPORT-CSV NewServerList.CSV | FOREACH { MySampleFileServer –nodename $_.Name }

 

And have that build out all of your .MOF files. But the coolest part of this is the end. To apply that configuration to all of the servers you only need execute this line.

 

START-DSCConfiguration –path .\MySampleFileServer –verbose

 

Pretty darn cool eh?

 

If you’re looking for more information on Desired State Configuration there are some excellent resources on Microsoft and Powershell.org. But if you’d like to same time on Bing here’s a Shortcut to that very list I have been building

If you find something of interest that has helped you with DSC ping me and I’ll add it to this resource list.

Until then

Remember, the Power of Shell is in YOU

Sean
the Energized Tech
Windows PowerShell MVP
Honorary Scripting Guy