From the course: PowerShell: Scripting for Advanced Automation

Working with DSC resources

From the course: PowerShell: Scripting for Advanced Automation

Start my 1-month free trial

Working with DSC resources

- [Instructor] The first building blocks of Desired State Configuration are the item potent scripts that set the configuration. These scripts or configurations call on resources to do the heavy lifting. Microsoft provides over a dozen resources with PowerShell by default. You can run the cmdlet Get-DSC resource in the terminal pane or in Windows PowerShell. And you can get more in the same way that you would get modules of functions. Modules of DSC resources are available on the PowerShell gallery or from the owners of some software that you might want to control as well. For this demonstration I'm going to use two of the built-in resources. I'm going to use the service resource so that we can configure one of the services on our Windows server. And I'm also going to use the Windows Feature resource to help us choose which roles or features may need to be installed or removed from our machines. Creating a configuration is quite a bit like creating a function. And in PowerShell ISE we're going to create two configurations. First, I'm going to go after one of the annoying services in Windows Server 2016. It's annoying to me because it always fails to start. When I go to the server manager I can always see one failed service and when I bring it up it's the Downloaded Maps Manager or MapsBroker. This is a service that always hangs up and I never use so I'm going to create a configuration that will make sure that this services is stopped. And that the start-up type is set to disabled. Back in PowerShell ISE I'm going to make the editing pane a bit bigger so that we can see what we're doing. And I'm going to start this configuration by typing the word configuration, just like we were creating a class or a function only this time it's a configuration and I need to assign it a name. I'm going to call this configuration StopMaps. And I'm going to place this entire configuration inside curly braces. The first thing I'm going to do inside these braces is import the DSC resource or the module that contains the DSC resources that I need. The module PSDesiredStateConfiguration is the one that holds all of the built-in resources. The next thing I need to do is specify which node we're about to manage. I'm going to set the node to localhost. But that could just as easily be done with a parameter at the top of the configuration. And we'll take a look at one of those in a little bit. Now, the next thing I need to do is add a set of curly braces and define what exactly I want to do with this specified node. Here is where you call the resource. I'm going to use the resource Service so that I could affect the MapsBroker service. And let me go ahead and call this block by a name. Let's go ahead and call it DisableMaps. And inside one more set of curly braces I'm going to give the configuration data that the service resource will use. There are only three things that I'm going to include here. The first is the name of the service. This is the first name in this configuration so far that has to be exactly right. It needs to be the name of the service that we're trying to affect and the things that I want to configure about the MapsBroker service are that the StartupType should be disabled and the State should be stopped. I could make a larger configuration that uses multiple resources to configure more than just this one service. Sometimes you create a complete configuration with all of the information you need about a single computer or you may create a configuration that can be applied across multiple computers that have consistent needs. For example, let me scroll down and show you another configuration that I created. This one could be used for all of the workstations in our network. This uses the WindowsFeature resource to make sure that BitLocker is always present and that the web server or IIS is always absent from our workstations. You'll notice that the node here was done using a variable and I specified that variable as a parameter so that it could be called when this configuration is run. I've given a default name in here but this could be left open or the default could be the name of one of our workstations. Using a parameter allows this configuration to be called for any computer name using this parameter opens up some options. In addition to allowing you to just type in a computer name when calling the configuration you could accept a comma separated list. Or you could accept piped input from the Get-ADComputer cmdlet. Or get content from some text file.

Contents