Monday, 28 August 2017

Learn PowerShell DSC - Part 1

Introduction

PowerShell DSC. What’s that? DSC stands for Dynamic State Configuration. It sounds like it could get a little complicated but if you’re just starting out using PowerShell to deploy configurations to machines then you’ll probably be lost in long scripts which check whether your configuration is correct then takes the steps to correct it.

To go to other parts of this series, see below:

You would have to write out the check statements and then write out all the logic to install the feature or copy the file etc.

With PowerShell DSC, you just state what you want your configuration to look like and DSC just “makes it so” and that’s the beauty of DSC.

image

Advantages of PowerShell DSC

As explained above, you need to write less code. That’s great but there’s more.

1) You can read through your code easier

This is called a configuration document. Rather than having a long script with if statements and lots of logic, you now just have a simpler script which states what you want the configuration to look like.

2) Look for configuration drift

DSC allows you to check a server against a configuration document to make sure there hasn’t been any changes.

3) Auto-remediation

DSC periodically checks a server’s configuration against the configuration document and then can be set to automatically restore the configuration to what it should be

4) Remote execution

You can deploy a configuration to remote machines and you can apply the same configuration to many machines

5) Centralised repository

You can configure your servers to pull their configuration from a central repository. This saves you having to copy scripts around and helps you manage versioning.

6) Works with workgroup servers

You’re not tied down to Kerberos authentication! You can deploy configurations to remote machines which are not on the domain.

7) Different teams can manage different configurations

This is called partial configurations. For example it allows your DBAs to manage the SQL related configuration while your systems team can manage the networking and your developers manage IIS.

8) One to many deployment

You can deploy the same configuration to more than one server at a time.

As you can see, DSC is really the best thing since sliced bread! It saves so much time and is a very elegant and efficient way to deploy configurations.

PowerShell DSC Example

Let’s take a look at a quick example. Let’s say we just want to make sure that an install file is copied to C:\Software on our server.

  • Server to configure: contlonsql01
  • Source file: \\contchisql01\Software\Installer.msi
  • Destination file: C:\Software\Installer.msi

Here’s what we’d write out to make this happen. Things to note:

  • configuration. This specifies that this is a PowerShell DSC configuration and the name is Configuration1.
  • Import-DscResource -ModuleName PSDesiredStateConfiguration. Perhaps you guessed it already but this imports the DSC resources from the DSC module. We’ll go into resources and modules another time so don’t worry about this line for now.
  • node is an array of servers we want to configure.
  • File. This specifies that we will be asking DSC to do a file or folder operation and we tell it what we want - i.e. we want to ensure that installer.msi is present which means it needs to be copied from the source location to the destination location.


image

configuration Configuration1 #Configuration1 is the name of the configuration
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration #Imports the DSC module

    node ("contlonsql01") #List the servers you'll be targeting. It'll deploy the settings within node {}
     {
         File InstallerFile #File is a DSC Resource
         {
            Ensure = "Present"
            SourcePath = "\\contchisql01\Software\Installer.msi"
            DestinationPath = "C:\Software\Installer.msi"          
         }
     }
}

There is no output from this. It’s like when you define a function:


image

Push a DSC configuration

Now we need to push the configuration to the machine. To do this we need to create a Management Object File or MOF file and we simply run the configuration and specify an output path for the MOF file:

Configuration1 -OutputPath C:\DS

image

Let’s take a look at the contents of C:\DSC

image

Note that the MOF file is named after the node specified in the configuration. If you have specified more than one node then you’ll get one MOF file per node.
We then deploy the configuration by using Start-DscConfiguration

Start-DscConfiguration -Path C:\DSC

image

Note that you don’t really get much output here. You just see that a job’s been started. if you want to get the output of the job, you can run use Get-Job:

Get-Job 11

image

So, it’s completed and we can confirm our install.msi file has been copied over:

image

We can also use Test-DscConfiguration to check our target machine configuration against the configuration we deployed to it:

Test-DscConfiguration -Path C:\DSC

image

Conclusion

So, that’s just a quick intro into PowerShell DSC. It’s such a great way to configure your servers and definitely the way of the future. In the next post, I’ll talk through DSC resources - these define what types of configuration settings you can make e.g. file, windows features, registry changes etc. Click here for part 2 to continue the DSC journey.

No comments:

Post a Comment