Exploring various configuration providers (Advanced)
A collection of configuration providers, which are known as resources, is a part of the core DSC system. These providers enable you to configure roles and features, copy files, create a registry entry, manage services, create local users and groups, and so on.
Getting ready
Each resource is technically represented by a DSC provider. The default location for these DSC providers is at C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSProviders
.
How to do it...
There are various DSC providers available. The following is the syntax for a few of them:
The following example uses the
Environment
resource to define or confirm the value of the specified environment variable:Environment MyEnv { Ensure ="Present" # You can also set Ensure to "Absent" Name ="MyEnvVariable" Value ="MyValue" }
The following example installs or verifies the installation of the specified feature:
WindowsFeature MyFeature { Ensure = "Present" Name = "MyFeatureName" }
You can get the list of DSC providers by using following command:
PS C:\> Get-DscResource
How it works...
As stated earlier, there are multiple resources available. I've provided information on some of these resources here.
Archive resources
The Archive
resource unpacks archive (.zip
) files at the given path.
Archive MyArchive
{ Ensure ="Present" # You can also set Ensure to "Absent"
Path ="C:\PS\MyScripts.zip"
Destination ="C:\PS\MyScripts" }
Group resources
The Group
resource manages local groups on the target machine.
Group MyGroup
{ Ensure ="Absent" # This will remove MyGroup, if present
GroupName="MyGroup" }
Package resources
The Package
resource installs and manages packages such as MSIs on the target machine.
Package MyPackage
{ Ensure ="Present"# You can also set Ensure to "Absent"
Path ="$FilePath\MySoftware.msi"
Name ="MyPackage" }
Registry resources
The Registry
resource manages registry keys and values.
Registry MyRegistry
{ Ensure ="Present" # You can also set Ensure to "Absent"
Key ="HKEY_LOCAL_MACHINE\SYSTEM\MyHiveKey"
ValueName ="RegName"
ValueData ="RegData" }
Script resources
The Script
resource defines ScriptBlock
that runs on target nodes. The TestScript
block runs first. If it returns False
, the SetScript
block starts running.
Script MyScript
{
SetScript = { # This block will run if TestScript returns False }
TestScript = { # This block runs first }
GetScript = { # This must return a hash table }
}
Service resources
The Service
resource manages services on the target machine.
Service MyService
{ Name ="MyService"
StartupType="Automatic" }
User resources
The User
resource manages local user accounts on the target machine.
User MyUser
{ Ensure ="Present" # To delete a user account, set Ensure to "Absent"
UserName ="MyName"
Password =$MyPassword # This needs to be a credential object
Requires ="[Group]MyGroup"# Configures MyGroupfirst }
There's more…
We can integrate any solution with DSC, and the minimal requirement is that you should be able to run the PowerShell script in such environments. You can create your own custom resources; we'll discuss this now.
Requirements for creating a custom DSC resource
To implement a DSC custom resource, create a new folder directly under \Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSProviders
. Rename the folder as your custom resource, and you need to declare the following three files into it:
MOF schema: The MOF schema defines the properties of the resource. To use your custom resource in a DSC configuration script, you assign values to these properties to indicate the configuration options. Then, save the MOF schema to a file called
CustomResourceName.schema.mof
.Script module: This defines the logical aspect of your resource. It consists of three functions:
Get-TargetResource
,Set-TargetResource
, andTest-TargetResource
. These functions take parameter sets as per the definition of the MOF schema file. Declare these three functions in a file calledCustomResourceName.psm1
. TheGet-TargetResource
function returns a hash table that lists all the resource properties as keys and the actual values of these properties as the corresponding values. Depending on the values that are specified for the resource properties in the configuration script,Set-TargetResource
must perform appropriate actions. Finally,Test-TargetResource
matches the status of the resource instance that is specified in the key parameters. It shows the Boolean output as eitherTrue
orFalse
based on the matching of key parameters.Module manifest: Finally, use the
New-ModuleManifest
CMDLET to declare aCustomResourceName.psd1
file for your new custom resource. DefineGet-TargetResource
,Set-TargetResource
, andTest-TargetResource
as a list of functions.