Monday, May 12, 2014

Using Windows PowerShell Desired State Configuration Archive 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

9.Using Registry Resource


In my previous blog post, we have seen the demo of
"Registry Resource of Desired state configuration".

Today we are going to use the "Archive Resource of Desired State Configuration'".

What Archive Resource Does?

It' simply unzip the zip files.It extracts them. That's all.

The usage of "Archive Resource" is very simple and straight forward. We only need to provide the Path to the Zip file which we want to extract, and the Destination path of the destination folder where we want to extract the contents of the zip file.

The Syntax of the "Archive Resource is " :

12-05-2014 14-42-08

Let's get started:

What we want to achieve?

We are going to do the following.

  • Copy from our sharing folder to Posh-Demo's "C:\ServerTools" folder. 
  • After copying, we will extract the contents of to the "C:\ServerTools\7zipExtracted" folder.
  • A very Simple process.

    We are back to our favourite server, Posh-Demo. You can see that, currently there are No file and no folder name as "7zipExtracted".


    12-05-2014 14-04-51


    Below is our simple DSC Script, the name of the script is "DSC_UnzippingDemo" and the name of the DSC Configuration is "unzippingDemo".

    In the 1st "File Resource" block we are copying the file from our share folder to the server.

    In our Second block, which is the "Archive Resource" block, we are providing the Path of file and in DestinationPath we are providing the path where we want to extract the file, our script the extracted folder path is "C:\ServerTools\7zipExtracted"

    Please also notice, that I have setup a dependency in the script, the "Archive Resource" is depends on "File Resource". 

    The logic behind is, we have to make sure that exists on the server, then only we are able to extract it, if doesn't exists on the Posh-Demo server, then our DSC script copy it over.

    I am not explaining the File resource block as we already explore it in details in our previous blog posts.

    In Archive Resource block,

    1. DependsOn : Dependency is set to File Resource
    2. Ensure : Present # Make sure it is present
    3. Path : Path of the zip file
    4. Destination : Where you want to extract the file


    12-05-2014 14-35-53


    Configuration unzippingDemo
    Node 'Posh-Demo'
    #Copying 7zip File First if it not exists

    File Copy7zip
    Ensure = 'Present'
    Type = 'File'
    SourcePath = '\\dc4-del\Tools\'
    DestinationPath = 'C:\ServerTools'
    Force = $true


    #Extracting the 7zip File

    Archive extract7zip
    DependsOn = "[File]Copy7zip"
    Ensure = 'Present'
    Path = 'C:\ServerTools\'
    Destination = 'C:\ServerTools\7zipExtracted'
    Validate = $true
    Force = $true


    Let's run the script.


    You can see that, we run the script and the MOF file is created.

    12-05-2014 14-20-58


    Lets' Deploy the MOF file, using Start-DscConfiguration cmdlet.

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


    12-05-2014 14-22-01

    You can see that, the deployment process is started.


    12-05-2014 14-22-43


    and it is finished pretty quickly.

    Now Let's check our Posh-Demo server and see , if the file get copied and get extracted.

    Here you go !

    Both, file and 7zipExtracted exists.

    12-05-2014 14-24-30

    Let's have a look inside the 7zipExtracted folder to check if our zip files get really extracted.

    Bingo!!! Yes they get extracted.

    Loved it !

    12-05-2014 14-25-04


    Just a quick note :

    If you use Validate = $true in the Archive folder , the output should be something look like this,

    12-05-2014 19-18-20

    Otherwise the output should be look like this.

    12-05-2014 14-26-52

    So use Validate parameter only if you also  declared the Checksum parameter.

    That's all for today, I hope you are enjoying this Desired State Configuration  tutorial Series,


    ThankYou (1)



    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

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