Get-Snapshot

As the command says this command shows you all the information you would like to know about Snapshots! Very useful for putting this command into a scheduled task and receiving an email form it reporting on your Snapshots.

Starting from the top, the command can not be run on its own. It needs information to be passed on by the Get-VM Command. For a list of all the snapshots in your environment run the following

Get-VM | Get-Snapshot

Both Get-VM and Snapshot have default wildcard capability so picks up everything in the criteria. Below is another example but narrowed down to 1 VM for display purposes.

Get-VM vmname | Get-Snapshot

So now to move on to an automated report, I will share the script and then analyse it line by line below.

add-pssnapin VMware.VimAutomation.Core
Connect-ViServer vCenterServer
$body = Get-VM | Get-Snapshot | Select Name, VM, SizeMb, Created | Out-String
send-mailmessage -From “myemail.com” -To “myemail.com” -SmtpServer “smtp.com” -Body $body

Now for the Line by line analyse

add-pssnapin VMware.VimAutomation.Core -> This line is to add the VMware Cmdlets into a normal Windows Powershell console to allow easier access to set-up theĀ Scheduled Task

Connect-ViServer vCenterServer -> As explained in my first ever post this command is to connect to a vCenter server or a Host from a CLI application

$body = Get-VM | Get-Snapshot | Select Name, VM, SizeMb, Created | Out-String -> We put our details in a variable “$body” for re-use later on | Get-VM issues a wildcard to collect all VM’s then passes that info through the pipeline | Get-Snapshot once again collects all snapshots for all VM’s and passes the data through the pipeline | We then Select the attributes we need (remembering that pipelines cancel the last pipe out e.g. During the selection we are not selecting any Get-VM attributes) Name of the Snapshot, VM name, Size in MB and Created Date | Out-String puts the object data into an array of strings

send-mailmessage -From “myemail.com” -To “myemail.com” -SmtpServer “smtp.com” -Body $body -> This line is self explainetry and requires input for the FROM email address, TO email address and SMTP Server. Body attribute is using the variable we created earlier for tidiness.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.