Powershell – DSC Custom Resource
Powershell Desired State Configurations (DSC)
Introduction
DSC is a technology developed for deploying and managing system configuration. And making sure it stays that way even if the configuration is change by other means than through DSC.
It has many similarities with AD Group Policies(GPO). The advantages of DSC are that it can manage Workgroup joined systems and it is much easier to write a custom DSC configuration than a GPO. It is also possible to manage Linux system by DSC.
DSC is based on the Managed Object Format (MOF) which is basically a configuration file like the ini-file.
PowerShell is just one way to create these mof-files. The advantage of using PowerShell is that the syntax for creating mofs is the very similar to writing a PowerShell function. The main difference is that instead of function it is called a configuration
Basic Powershell function
function verb-noun { ($param1, $param2) Commandlet -Paramater $param1 }
Basic Powershell DSC configuration
configuration myconfig { ($param1, $param2) <# The Node block represents the target computer There can be zero or several Node blocks. The node can also have multiple computer targets #> Node "localhost" { <# The Resource Block defines what resource that is being configured There can be serverel resource blocks per node #> WindowsFeature NoTelnetServers #First the resourcetyp then a identifying name { Ensure = "Absent" # Absent to uninstall the feature, Present to install Name = Telnet-Server # Name of the feature to uninstall/install } } }
Applying a configuration
To apply the configuration, the mof first have to be generated.
This is done simply by running the configuration just like a function.
. .\myconfigscript.ps1 Myconfig
This will create a folder with the name of the configuration and a mof file for each node specified.
To apply the mof use the Start-DscConfiguration cmdlet. This will use Powershell Remoting (WinRM service) to deploy the configuration to each computer.
#The wait parameter tells the cmdlet to output the progress to the screen instead of running it in the background as a job. Start-DscConfiguration -Verbose -Wait -Path .\myconfig