Saturday, 2 September 2017

Learn PowerShell DSC - Part 2

Introduction

In part 1, we went through how to make a simple configuration using the File DSC resource and then how to push it out to a single computer. In this part, we’ll go through how to learn about the available DSC resources and demo a few of them.

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

List DSC Resources

Windows has a number of DSC resources built in and you can view the list by using Get-DscResource:

Get-DscResource -Module PSDesiredStateConfiguration

image

Get help for a DSC Resource

Now, let’s say you think you need to use the Service DSC resource but you don’t know the syntax, you can use Get-DscResource:

Get-DscResource Service -Syntax

image

You can also use Get-DscResource to get information about what properties you can set:

Get-DscResource Service | % Properties

image

Service DSC Resource Example

As we’ve just seen the syntax of how to use this DSC resource, let’s start with this.

My target machine has the Windows Audio service stopped and the service is set to manual start:

Get-Service Audiosrv | fl *

image

I want to set this to start automatically and to also start the service so the configuration I’d use is below. (Remember to import the correct module that has your DSC resource - in our case it’s the PSDesiredStateConfiguration module):

configuration Service
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {
         Service WindowsAudio
         {
             Name = "audiosrv"
             StartupType = "Automatic"
             State = "Running"
         }
     }
}

image

Run your configuration - you should see no output from this.

As in part 1, we create a MOF file:

Service -OutputPath C:\DSC\Service

image

…….then push the configuration to our target machine contchidsc01:

Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Service -Wait -Verbose -Force

image

……and then we confirm that our target machine matches the configuration:

Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Service

image

We can also check the service to make sure it’s started and start up is automatic:

Get-Service Audiosrv | fl *

image

Archive DSC Resource Example

This allows you to extract a zip file from a location into a location on the target machine. See the example below:

configuration Archive
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         Archive Unzip
         {
             Destination = 'C:\Software\Docker'
             Path = '\\contchisql01\Software\Docker.zip'
             Checksum = 'SHA-256'
             Validate = $true
             Force = $true
             Ensure = 'Present'
         }
     }
}

Archive -OutputPath C:\DSC\Archive
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Archive -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Archive

image

Environment DSC Resource Example

This DSC resource adds environment variables. In this case, it creates a new variable called NewVar and gives it a value of ‘Value to store’.

configuration Environment
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         Environment NewVar
         {
             Name = 'MYNEWVAR'
             Value = 'Value to store'
             Ensure = 'Present'
         }
     }
}

Environment -OutputPath C:\DSC\Environment
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Environment -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Environment

image

Group DSC Resource Example

This DSC resource creates a local group and can set the members for it too. This example creates a group called TestGroup and makes Administrator a member of it.

configuration GroupConfig
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         Group TestGroup
         {
             Ensure = 'Present'
             GroupName = 'TestGroup'
             Description = 'This is a DSC test group'
             Members = 'administrator'
         }
     }
}

GroupConfig -OutputPath C:\DSC\Group
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Group -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Group

image

Process DSC Resource Example

This DSC resource starts or stops a particular process. You can also specify the arguments for the process too. This example ensures that notepad.exe is running.

configuration ProcessConfig
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         WindowsProcess MSPaint
         {
             Path = 'notepad.exe'
             Arguments = ''
         }
     }
}

ProcessConfig -OutputPath C:\DSC\Process
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Process -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Process

image

Registry DSC Resource Example

You can use this DSC resource to create, delete or set registry keys and values. This example creates a registry key 'HKEY_Local_Machine\Software\DSCTest' with the below value:

  • Name: DSCTestGood
  • Value: True
  • Type: REG_SZ (string)

configuration RegistryConfig
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         Registry CreateReg
         {
             Key = 'HKEY_Local_Machine\Software\DSCTest'
             ValueName = 'DSCTestGood'
             ValueType = 'string'
             ValueData = 'True'
         }
     }
}

RegistryConfig -OutputPath C:\DSC\Registry
Start-DscConfiguration -Computername contchidsc01
-Path C:\DSC\Registry -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Registry

image

Conclusion

As you can see, you can do a lot with PowerShell DSC and these are just a few of the built in DSC resources you can use. There’s a lot more on GitHub and we’ll come to this in part 4. Stay tuned for part 3 where we’ll start using parameters and a setting called DependsOn.

No comments:

Post a Comment