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:
- Learn PowerShell DSC Part 1
- Learn PowerShell DSC Part 3
- Learn PowerShell DSC Part 4
- Learn
PowerShell DSC Part 5 - Learn
PowerShell DSC Part 6 - Learn
PowerShell DSC Part 7
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
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
You can also use Get-DscResource to get information about what properties you can set:
Get-DscResource Service | % Properties
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 *
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”
}
}
}
Run your configuration – you should see no output from this.
As in part 1, we create a MOF file:
Service -OutputPath C:DSCService
…….then push the configuration to our target machine contchidsc01:
Start-DscConfiguration -Computername contchidsc01 -Path C:DSCService -Wait -Verbose -Force
……and then we confirm that our target machine matches the configuration:
Test-DscConfiguration -ComputerName contchidsc01 -Path C:DSCService
We can also check the service to make sure it’s started and start up is automatic:
Get-Service Audiosrv | fl *
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:
- Source zip file: \contchisql01SoftwareDocker.zip
- Destination folder: C:SoftwareDocker
configuration Archive
{
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node contchidsc01
{
Archive Unzip
{
Destination = ‘C:SoftwareDocker’
Path = ‘\contchisql01SoftwareDocker.zip’
Checksum = ‘SHA-256’
Validate = $true
Force = $true
Ensure = ‘Present’
}
}
}
Archive -OutputPath C:DSCArchive
Start-DscConfiguration -Computername contchidsc01 -Path C:DSCArchive -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:DSCArchive
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:DSCEnvironment
Start-DscConfiguration -Computername contchidsc01 -Path C:DSCEnvironment -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:DSCEnvironment
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:DSCGroup
Start-DscConfiguration -Computername contchidsc01 -Path C:DSCGroup -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:DSCGroup
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:DSCProcess
Start-DscConfiguration -Computername contchidsc01 -Path C:DSCProcess -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:DSCProcess
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_MachineSoftwareDSCTest’ 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_MachineSoftwareDSCTest’
ValueName = ‘DSCTestGood’
ValueType = ‘string’
ValueData = ‘True’
}
}
}
RegistryConfig -OutputPath C:DSCRegistry
Start-DscConfiguration -Computername contchidsc01 -Path C:DSCRegistry -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:DSCRegistry
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.