PowerShell DSC – Registry Example

email me

Show more about DSC...

 

Using a DSC script, create a registry key, start configuration, and view configuration.

The basic processing works like this:

make a declarative script > generate mof > apply mof > verify configuration is running


Code

Set-Location "C:\DSC\"

Configuration RegistryExample
{
Node localhost
{
Import-DscResource -ModuleName PSDesiredStateConfiguration
Registry RegistryExample
{
Ensure      = "Present"  # Present, Absent
Key         = "HKEY_LOCAL_MACHINE\SOFTWARE\ExampleKey"

ValueName   = "ValueHere"
ValueData   = "DataHere"
}
}
}

# create mof
RegistryExample

# load configuration from mof
Start-DscConfiguration -Path RegistryExample -ComputerName localhost -Wait -Verbose -Force

# check configuration
Get-DscConfiguration

 

+ ValueType = “String”
+ Force         = $true


Notes

Install-Module -Name PSDesiredStateConfiguration
winrm quickconfig

Desired State Configuration Overview for Engineers

PSDesiredStateConfiguration

Get-DscConfiguration

DSC mofs C:\Windows\System32\Configuration

 

Contents of the mof file

/*
@TargetNode='localhost'
@GeneratedBy=Homelab
@GenerationDate=04/15/2019 19:13:08
@GenerationHost=HOMELAB
*/

instance of MSFT_RegistryResource as $MSFT_RegistryResource1ref
{
ResourceID = "[Registry]RegistryExample";
ValueName = "ValueHere";
Key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\ExampleKey";
Ensure = "Present";
SourceInfo = "C:\\DSC\\reg_example.ps1::8::2::Registry";
ModuleName = "PSDesiredStateConfiguration";
ValueData = {
"DataHere"
};

ModuleVersion = "1.0";

ConfigurationName = "RegistryExample";

};
instance of OMI_ConfigurationDocument

{
Version="2.0.0";

MinimumCompatibleVersion = "1.0.0";

CompatibleVersionAdditionalProperties= {"Omi_BaseResource:ConfigurationName"};

Author="Homelab";

GenerationDate="04/15/2019 19:13:08";

GenerationHost="HOMELAB";

Name="RegistryExample";

};