PowerShell script – List snapshots

By | January 28, 2009

I have created at PowerShell script that obtains a list of VM’s that have snapshots attached. The output is then converted to a html file using the cmdlet ConvertTo-HTML.
And just to make it easier to obtain the html file, the script mails the html file to me as an attachment.

Ths script has been updated thanks to Ryan, to be able to handle multiable snapshots on each VM.

Get the script from the Downlaod section.

9 thoughts on “PowerShell script – List snapshots

  1. Spacir

    Hallo.
    Good Job. Script is OK, but not work fine for 2 or more snapshots on one VM. Result is
    if ($snapshots.Length -ge 1){

    Bye SPACIR.

  2. Ryan

    To list VM’s with 1 or more snapshots I made the following change:

    if ($snapshots.Name.Length -ige 1 -or $snapshots.length){

  3. John

    I really liked your script, but found that the following changes to your script were helpful for our group, so I thought that I would pass them along.

    Adding a date to the subject line:
    $strSubject = “Snapshot list – ” + (get-date -DisplayHint date)

    Since we are using Outlook I removed the $strBody and replaced the message body with the html you generated. That way I did not have to open the attachment to “see” the results:
    $msg.IsBodyHtml = 1
    $msg.Body = Get-Content $strOutFile

    If you would prefer to skip creating the output file you can remove the appropriate lines and change the above $msg.body to:
    $msg.Body = ($myCol | Sort-Object VM | ConvertTo-HTML -Head $head -Body $strMail)

    I also found a interesting article from Marc Lognoul on his Straight from the Cask! blog that adds the bell icon for those of us using Outlook:
    $msg.Headers.Add(“message-id”, “<3BD50098E401463AA228377848493927-1>”)

    Finally one comment, since Ryan provided you the code for dealing with the multiple snapshots, shouldn’t he also be added into the script header and be given the appropriate credit?

  4. A. Mikkelsen Post author

    Hi John,
    I have updated your post and the script.
    Many thanks for the updates – I love them 🙂
    This will make the script even better.

  5. Clark Dickson

    Hello there, your script is great I use it all the time. Is there a way to have this script run indefinite.

  6. Cem

    Hi,
    I edit your script and wish get snapshot list from txt file.
    # Get the list of VM’s
    $vms = Get-Content “c:\list.txt”
    Its run and send mail but on the mail i cant see any vm computer name …
    Snapshot name, created and description cells are ok .
    where make i mistake ?

  7. A. Mikkelsen Post author

    Hi Cem,
    The problem is that “$vms = Get-VM” from the original script is a collection of information for each VM. And the “$vms = Get-Content “c:\list.txt”” you are using, is only a list of vm names.

    If you want it to work, replace all from
    “# Get the list of VM’s
    $vms = Get-VM” with the below.

    $vmlist = Get-Content “c:\list.txt”

    $myCol = @()
    ForEach ($vm in $vmlist){
    $myVM = Get-VM -Name $vm
    $snapshots = Get-SnapShot -VM $myVM
    if ($snapshots.Name.Length -ige 1 -or $snapshots.length){
    ForEach ($snapshot in $snapshots){
    $myObj = “” | Select-Object VM, Snapshot, Created, Description
    $myObj.VM = $$myVM.name
    $myObj.Snapshot = $snapshot.name
    $myObj.Created = $snapshot.created
    $myObj.Description = $snapshot.description
    $myCol += $myObj
    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *