PowerCLI DSC Resource for Snapshot check.

PowerCLI DSC Resource for Snapshot check.

PowerCLI DSC Resource for Snapshot check.

Finally after a week of exploring DSC and building custom resource modules, i was finally able to build a custom DSC resource using PowerCLI.

Huge Thanks to my friend Ravi, MVP PowerShell for helping me out with my queries.You can see nice DSC and powershell posts from him at http://www.powershellmagazine.com

The PowerCLI DSC Resource which i created check’s for configurations of snapshots on a set of vm’s in a resource pool, and based on the condition “Ensure = Present” / “Ensure = Absent” takes the appropriate actions of creating snapshots on the VM’s / removing all the existing snapshots.

I built this resource to share how we can integrate DSC with PowerCLI to build powerful tools for configuration management, this resource can be effectively used to remove snapshots on VM’s if they are left to exist and also admins tend to forget after the VI infrastructure change activities.

Next let me go through a step by step process of how i built this resource using powercli, although resources can be also built using C# etc, i wanted to show how an IT pro can also start building his own resources.

So let me start with the first step of building your resources and that is to make yourself a Resource Schema MOF file.

In an ISE editor or even a notepad, type in the below and create the MOF Files. The class consists of the properties which need to be passed as parameters to the resources. i my DSC resource i have the vcentername, the resourcepool name and also the DomainAdmin credentials to connect to the vcenter.

Once you create the schema mof file, save it as “DSC_PowerCLISnapShotCheck.schema.mof”.

Next lets move on to the actual PowerShell module which does all the heavy lifting, to create a DSC resource we need to have 3 set of functions which are the “Get-TargetResource”, “Set-TargetResource” and “Test-TargetResource”.

When ever a DSC configuration is invoked, the Test-TargetResource tests the system as per the configuration specified by the user, based on the conditions specified it calls the Set-TargetResource to take an appropriate action. This was in short , For more details on these please refer to a nice article at PowerShell.org

So here is the actual function which has the logic build in and does all the “Make It Happen” :).

Once you create the script module / .psm1 file which contains all the logic, save it as “DSC_PowerCLISnapShotCheck.schema.psm1”.

Once we have the script module created, next we need to generate the module manifest for the script module, this can be done via the New-ModuleManifest cmdlet.

Once you generate all the required files, save each of the DSC resource components specified as per the below location.

PowerCLI DSC Resource

Here’s a graphical view of how the module files are placed.


Once all the steps are done, lets head onto the grand finale 🙂 of invoking this DSC resource, i would be using the push method and the configured host will be the localhost itself, also note that you need to have PowerCLI installed on your system, you can check out one of my earlier post on DSC with PowerCLI to configure PowerCLI via DSC.

Ok so lets see the configuration which would be used to push the configuration across the systems, note that in production environment you would need to use certificate based authentication as its more secure.

Next lets push the configurations using the below command.

Now in the above configuration we have kept Ensure = “Absent”, which when executed will make sure that if there are snapshots on the vm’s it would be deleted.

Here are some screen captures from the resource execution aka final victory in the war against DSC 🙂 .


Well, that’s the end of this blog post folks,hope you enjoyed it and gives you ideas to build your own custom DSC resources using PowerCLI.

PowerCLI DSC Resource

In the end always remember DSC resources are just PowerShell Modules :), and if you have the logic, you can build anything!

You can also download the Resource from PowerShell gallery

Do Check out my other posts on DSC and PowerCLI

Please follow and like us:

Share this post

One thought on “PowerCLI DSC Resource for Snapshot check.

Post Comment