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 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 " :
Let's get started:
What we want to achieve?
We are going to do the following.
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".
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 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,
- DependsOn : Dependency is set to File Resource
- Ensure : Present # Make sure it is present
- Path : Path of the zip file
- Destination : Where you want to extract the file
Configuration unzippingDemo
{
Node 'Posh-Demo'
{
#Copying 7zip File First if it not exists
File Copy7zip
{
Ensure = 'Present'
Type = 'File'
SourcePath = '\\dc4-del\Tools\7zip.zip'
DestinationPath = 'C:\ServerTools'
Force = $true
}
#Extracting the 7zip File
Archive extract7zip
{
DependsOn = "[File]Copy7zip"
Ensure = 'Present'
Path = 'C:\ServerTools\7zip.zip'
Destination = 'C:\ServerTools\7zipExtracted'
Validate = $true
Force = $true
}
}
}
Lets' Deploy the MOF file, using Start-DscConfiguration cmdlet.
Start-DscConfiguration -Path .\unzippingDemo -Wait -Verbose
You can see that, the deployment process is started.
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.
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 !
Just a quick note :
If you use Validate = $true in the Archive folder , the output should be something look like this,
Otherwise the output should be look like this.
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
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
Note: Only a member of this blog may post a comment.