Quantcast
Channel: Aman Dhally's Blog
Viewing all articles
Browse latest Browse all 10

Using Windows PowerShell Desired State Configuration Archive Resource

$
0
0

 

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 7zip.zip from our sharing folder to Posh-Demo’s “C:\ServerTools” folder. 
  • After copying 7zip.zip, we will extract the contents of  7zip.zip 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 7zip.zip 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 isunzippingDemo”.

    In the 1st “File Resource” block we are copying the 7zip.zip file from our share folder to the server.

    In our Second block, which is the “Archive Resource” block, we are providing the Path of 7zip.zip file and in DestinationPath we are providing the path where we want to extract the 7zip.zip 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 7zip.zip exists on the server, then only we are able to extract it, if 7zip.zip 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

     

    <font size="4" face="Calibri">Configuration unzippingDemo<br>{<br>    Node <span style="color: #008000">'Posh-Demo' </span><br>    {<br>        #Copying 7zip File First <span style="color: #0000ff">if</span> it <span style="color: #0000ff">not</span> exists<br>        <br>        File Copy7zip <br>        {<br>            Ensure = <span style="color: #008000">'Present'</span><br>            Type = <span style="color: #008000">'File'</span><br>            SourcePath = <span style="color: #008000">'\\dc4-del\Tools\7zip.zip'</span><br>            DestinationPath = <span style="color: #008000">'C:\ServerTools'</span><br>            Force = $<span style="color: #0000ff">true</span><br><br>        }<br><br>        #Extracting the 7zip File<br><br>        Archive extract7zip<br>        {<br>            DependsOn = <span style="color: #006080">"[File]Copy7zip"</span><br>            Ensure = <span style="color: #008000">'Present'</span><br>            Path = <span style="color: #008000">'C:\ServerTools\7zip.zip'</span><br>            Destination = <span style="color: #008000">'C:\ServerTools\7zipExtracted'</span><br>            Validate = $<span style="color: #0000ff">true</span><br>            Force = $<span style="color: #0000ff">true</span><br>        }<br>    }<br>}</font>
     
    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 7zip.zip file get copied and get extracted.

    Here you go !

    Both, 7zip.zip 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,

    Thanks

    ThankYou (1)

     

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


  • Viewing all articles
    Browse latest Browse all 10

    Trending Articles