VMware Cloud Community
mpengle1
Contributor
Contributor
Jump to solution

Snapshot script for multiple snapshots? Example in post.

Hello,

I'm hoping somebody can lend me a hand or give me some direction.  There are a lot of snapshot monitoring scripts out there, we use a few of them in our environment.  What I'm looking to do though, is run a script that will find all virtual machines that have more than one snapshot and email a report out.  The script below looks like it would be a good start, but I don't have enough knowledge in PowerCLI to have it report only VMs with multiple snapshots.  I'm sure I can figure out the email portion but the first part I'm looking for a little help.

$Report = Get-VM | Get-Snapshot | Select VM,Name,Description,@{Label="Size";Expression={"{0:N2} GB" -f ($_.SizeGB)}},Created

If (-not $Report)

$Report = New-Object PSObject -Property @{

      VM = "No snapshots found on any VM's controlled by $VIServer"

      Name = ""

      Description = ""

      Size = ""

      Created = ""

   }

}

$Report = $Report | Select VM,Name,Description,Size,Created | ConvertTo-Html -Head $Header -PreContent "<p><h2>Snapshot Report - $VIServer</h2></p><br>" | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd

Thanks!

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this.

After a Group-Object, the individual objects are to be found under the Group property.
From each entry we extract the name, and then join them together into one string.
That way there will be no issues with for example an Export-Csv

Get-VM | Get-SNapshot | Group-Object -Property {$_.VM.Name} |

where{$_.Count -ge 2} |

Select @{N='VM';E={$_.Name}},@{N='SnapshotNr';E={$_.Count}},

    @{N='SnapshotNames';E={$_.Group.Name -join ','}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

To only select the VMs that have 2 or more snapshots, you could do something like

Get-VM | Get-SNapshot | Group-Object -Property {$_.VM.Name} |

where{$_.Count -ge 2} |

Select @{N='VM';E={$_.Name}},@{N='Snapshots';E={$_.Count}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

mpengle1
Contributor
Contributor
Jump to solution

Thanks so much, LucD.  That ran and outputted the detail I needed perfectly.  One more question:

@{N='VM';E={$_.Name}},@{N='Snapshots';E={$_.Count}

What would the variable be if I wanted to also include the snapshot names (Name1, Name2) and possibly the total size?  I'm searching around but not finding a lot that is understandable to me.

I appreciate the help!

Thanks.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this.

After a Group-Object, the individual objects are to be found under the Group property.
From each entry we extract the name, and then join them together into one string.
That way there will be no issues with for example an Export-Csv

Get-VM | Get-SNapshot | Group-Object -Property {$_.VM.Name} |

where{$_.Count -ge 2} |

Select @{N='VM';E={$_.Name}},@{N='SnapshotNr';E={$_.Count}},

    @{N='SnapshotNames';E={$_.Group.Name -join ','}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

mpengle1
Contributor
Contributor
Jump to solution

This is exactly what I was looking for, thank you!

0 Kudos