Finding out which VM’s have snapshots using VMware PowerCLI

A few weeks ago at VMworld Europe, I attended “The Power Hour: vSphere PowerCLI 10th Birthday Edition” breakout session with Alan Renouf and Luc Dekens.  In the audience was the Product Manager for VMware PowerCLI, Jake Robinson.

After the session, I got talking to Jake and noticed he was wearing a cool PowerCLI 10th Birthday t-shirt so I asked him how could I get one – he told me that they were not just giving these away and that I needed to make a contribution to the PowerCLI-Example-Scripts repository on Github.

I decided to accept the challenge as I have worked on a few PowerCLI scripts in the past but never had the courage to publish these to the general community.  Originally Jake gave me a deadline of the end of VMworld to get my code submitted but unfortunately due to my schedule and also some last minute compiling issues with the code I missed the deadline.  However, a few days later I was able to dedicate time to this and duly submitted my code for review via a pull request (any submission to the PowerCLI-Example-Scripts repository has to be vetted for content and conforming to published guidelines).

A few anxious weeks went by as I was thinking; is my code good enough? did I miss something? could I have done this better? but then last week I received an e-mail from Kyle Ruddy stating that my submission and pull request had been accepted.

The script vCenterSnapShot.ps1 is now available to download from the PowerCLI-Example-Scripts Repository.

Script Breakdown

$vCenter = Read-Host -prompt ‘Enter FQDN / IP address of vCenter’
This creates the variable $vCenter and stores entered value for either FQDN / IP address of the vCenter.

if ($vCenter) {   
          $vcUser = Read-Host -prompt ‘Username’
If value for vCenter is entered then user is prompted for username to access vCenter which is then stored in variable $vcUser.

vcsnapshot_enterdetails

Connect-VIServer -Server $vCenter -User $vcUser -Password VMware123
Here we take the variables $vCenter and $vcUser and combine them into a string to connect to the vCenter.

Note: For this script the Password was hard-coded as “VMware123” but this can be changed by the user.

vcsnapshot_retrievingdata

$getSnap = Get-VM | Get-Snapshot | sort SizeGB -descending | Select VM, Name, Created, @{Label=”Size”;Expression={“{0:N2} GB” -f ($_.SizeGB)}}, Id
This is where the bulk of the script processing happens, we firstly create the variable $getSnap.  Using the PowerCLI cmdlet “Get-VM” and piping this to Get-Snapshot cmdlet we are able to pull snapshot data from the vCenter using the vSphere API.

Within Get-Snapshot cmdlet we sort the data by descending order in GB using the code “sort SizeGB -descending” and then select which columns we want to output, in this case VM, Name, Created, Size and Id.

To make the output more readable we round down the GB size to 2 decimal places using this code “@{Label=”Size”;Expression={“{0:N2} GB” -f ($_.SizeGB)}}”

$getSnap | Format-Table | Out-Default
We take the output stored in $getSnap variable and output this into a table displayed on screen using Format-Table.

vcsnapshot_.tableoutput.png

Disconnect-VIServer $vCenter -Confirm:$false
Finally we gracefully end the active session to the vCenter.

The last step of the script is to exit the if condition by displaying an error message if no value is displayed for the vCenter.

vcsnapshot_error

Conclusion

You might be thinking this is a lot of effort just to get a commemorative t-shirt,  however I wanted to also contribute back to the community and with this script I have started to do that.  Now I just need to work on creating more elaborate scripts using PowerCLI and to follow up with Jake on that t-shirt! 🙂

Thanks for reading and please feel free to leave a comment or message me on twitter (@lukaswinn) if you found this article useful.

–Lukas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: