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”.

Wednesday, May 7, 2014

PowerShell and DSC : Using File Resource

 

 

·         Introduction of Windows PowerShell “Desired State Configuration”.

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

·         Getting Started With Desired State Configuration: DSC Syntax.

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

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

·         Run your first "DSC" PowerShell Script.

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

 

Today, we are going to use “File Resource” of the “Desired State Configuration”.

If you have some standards tools or executable, which you love to keep on your every server, then this is a very good DSC resource. it is very useful for making sure, some files , folder which you want get copied.

I have a “ServerTools” folder in all of my servers, this folder contains some small handful utilities. 

Today we are going to write a “Desired State Configuration Script” which create this folder on the server automatically, for achieving this obviously we are going to use our “FILE Resource”.

 

Syntax of the File Resource

 

image

 

Let’s get started.

You can see in the screenshot of my server “Posh-Demo” that there is no “ServerTools” folder exists yet.

clip_image001

 

The below screenshot is from my local share folder in my network , in which I do keep all of the Server Tools.

 

clip_image003

This is our configuration script, it’s very simple.

----------------------------------------------------------

Configuration createServerToolsFolder

{

    Node 'Posh-Demo'

    {

        File copyFolder

        {

            Type = "Directory"

            Ensure = 'Present'

            Recurse = $true

            SourcePath = '\\Server-1\Tools\ServerTools'

            DestinationPath = 'C:\ServerTools\'

            Force = $true

 

        }

    }

}

createServerToolsFolder

 

--------------------------------------------------------------------------

 

You can see that script is very simple., You may already aware from “Configuration” and “Node” block. In file Block I have declare the following prameters.

Type = Directory # That’s means it will create a Directory, you can also set it to File, if you are copying any file.

Ensure = Present # That’s means , this folder has to be present on our server.

Recurse = $true # This will copy the root folder and it’s subfolder and files too.

SourcePath = \\Server-1\Tool\ServerTools # the Source path of the folder which you want to copy..

Destination = C:\ServerTools # Location on you configuring server, that where you want to copy folder to.

Force = $true # If the folder exists overwrite it.

 

clip_image005

 

Lets run the Script.

We run the script and you can see, it has exported the MOF file.

clip_image007

Now’ let’s start deploying it,

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

 

clip_image009

 

 

Oop! It’s not completed successfully, it give us an error about

The path cannot point to the root directory or to the root of a net share.

And

SourcePath must be specified if you want to configure the destination directory recursively. Make sure that SourcePath is a directory and that it is accessible

 

clip_image011

 

What happen? Why it not able to Copy the files from local share to the Server?, we run the ISE as “Administrator” it should be working! No?

NO!

The problem is ( thanks to PowerShellMagazine.com) DSC Local Configuration Manager runs as the SYSTEM account and won’t have access to network resources. We have to give permission to them.

To solve this, we have to add both, the client computer and the Sever computer account to the network share directory and give them read only access.

How to add Computer account to the share?

It’s simple, Right click on your Share Folder, Click on Properties, in Security Tab, click on edit, in Object Type , select “Computers”, click on “OK” and then in “enter the Object Name Box”, type your both computer names and add them.

clip_image013

And give them, Read and List Folder contents permission only.

 

clip_image014

 

Now, try to deploy the mof file again.

And you can see, that we didn’t get any error this time. The folder get copied successfully.

 

clip_image016

 

Let’s check if the “ServerTools” folder is get copies to the server.

Yes it is J

clip_image018

That’s all for today, I hope that you may find it useful

 thank-you-image

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”.