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

No comments:

Post a Comment