Thursday, May 8, 2014

Using Windows PowerShell Desired State Configuration Registry Resource

 

1.  Introduction of Windows PowerShell “Desired State Configuration”.

2.  Installing Windows PowerShell 4.0 [Windows Management Framework 4.0].

3.  Getting Started With Desired State Configuration: DSC Syntax.

4.  Review of Desired State Configuration: The 3 easy steps.

5.  Write your First Desired State Configuration Script using ROLE Resource.

6.  Run your first "DSC" PowerShell Script.

7.Configuring Dependencies in "Desired State Configuration" script in PowerShell.

8.PowerShell and DSC : Using File Resource

 

In my previous blog post, we have seen how to use, "Desired State Configuration File Resource".

Today we are going to use "Desired State Configuration Registry Resource".

The registries are a very good way to keep the information.

I have created my own Registry keys folder on my local servers, in which I do keep a track of the various information for example, when the server was build, in which geographical location is the server located etc..

The Syntax of "Registry Resource" is.

08-05-2014 19-05-12

Let's get started.

We are going to write a new DSC script today. The name of the script is "SettingRegistry".

In this DSC configuration script, we are created two Registry Resource Script block.

In first script block, we are creating  a new Registry Key folder name as "IT Internal" and adding a new Key name as "Geographical Location" and setting it's value to "New Delhi, India".

In our second script block, we are creating a another key name as "ServerBuildversion" and setting the key value to "2.0".

We will deploy this configuration to our favourite guinea pig server "Posh-Demo".

You can check in the below screenshot, that currently there is NO key exists as the name of "ITinternal".

08-05-2014 12-16-31

You can check the below DSC script. The script is very simple to understand.

 

Ensure = Present # That means it has to be there.

Key = "HKEY_LOCAL_MACHINE\Software\ITInternal" # The path to the Key, if the ITinternal key folder  doesn't exists, it will get created automatically.

ValueName = "Geographical Location" # The Name of the Key which you want to Create.

ValueData = "New Delhi, India" # the value of the ValueName key, which we have mentioned above.

Force = $true # if the key already exists overwrite it.

ValueType = "String" # What kind of value we are storing, the valid options are,  { Binary | Dword | ExpandString | MultiString | Qword | String }

 

My script.


Configuration settingRegistry
{
Node 'Posh-Demo'
{
Registry serverGEOlocation
{
Ensure = 'Present';
Key = 'HKEY_LOCAL_MACHINE\Software\ITInternal'
ValueName = 'Geographical Location'
ValueData = 'New Delhi, India'
Force = $true
ValueType = "String"


}


Registry ServerBuid
{
Ensure = 'Present';
Key = 'HKEY_LOCAL_MACHINE\Software\ITInternal'
ValueName = 'ServerBuildVersion'
ValueData = '2.0'
Force = $true
ValueType = "String"

}



}
}

settingRegistry
You can see that our two script blocks clearly marked below. 

08-05-2014 12-17-10


 


Now, run the script. and it will export the MOF file.


08-05-2014 12-19-51


Start deploying it using Start-DscConfiguration cmdlet.


Start-DscConfiguration -Path .\settingRegistry -Wait -Verbose


08-05-2014 12-23-22


You can see that  it finishes deploying MOF file, and that's in just 2.23 seconds. Wow! 


08-05-2014 12-24-10


Now, let's check our server and see if the desired registry entries get created or not.


You can see in below screenshot of the server,  that all registry keys, which we configured in DSC script are created successfully.


We all love "Desired State Configuration", Isn't Smile 


08-05-2014 12-24-48



I am hoping that you may find this blog postings useful.


ThankYou (2)


Regards


Aman Dhally


If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

2 comments:

  1. Hi...I am trying to add a registry value under HKCU, DSC ends up adding it to HKUsers instead of HKCU. This happens even when I try using the script resources. Any ideas? Updating the HKCU registry is a must in my case.

    ReplyDelete
    Replies
    1. Its adding this setting in the HKusers for the System account since this is what the DSC runs as.

      Delete

Note: Only a member of this blog may post a comment.