Find virtual machine snapshots with PowerCLI

2 10 2011

Run from a PowerCLI session connected to a vCenter environment to find and list all of the snapshots (and users  who took them, which Get-VM | Get-Snapshot won’t do) on your managed ESX/ESXi hosts:

$myVMs = Get-VM
$VMsWithSnaps = @()
foreach ($vm in $myVMs) {
    $vmView = $vm | Get-View
    if ($vmView.snapshot -ne $null) {
        Write-Host "VM $vm has a snapshot"
        $SnapshotEvents = Get-VIEvent -Entity $vm -type info -MaxSamples 1000 | Where { 
            $_.FullFormattedMessage.contains("Create virtual machine snapshot")}
        try {
        $user = $SnapshotEvents[0].UserName
        $time = $SnapshotEvents[0].CreatedTime
        } catch [System.Exception] {
            $user = $SnapshotEvents.UserName
            $time = $SnapshotEvents.CreatedTime
        }
        $VMInfo = “” | Select "VM","CreationDate","User"
        $VMInfo."VM" = $vm.Name
        $VMInfo."CreationDate" = $time
        $VMInfo."User" = $user
        $VMsWithSnaps += $VMInfo
    }
}
$VMsWithSnaps | Sort CreationDate
Advertisements

Actions

Information

5 responses

23 11 2011
Christian

Thanks! That was very helpful – I needed a script for listing all snapshots and just found that snippet. I added a few lines to also list the name and description of the snapshot. As a tipp – for the german version of ESXi 4+ you need to replace the string “Create virtual machine snapshot” with “Snapshot der virtuellen Maschine erstellen” in the contains() function (line 7).

If you’re interested in my modified script – have a look here: http://pastebin.com/q8mKR02g

Greetings!

16 04 2013
TSR

This is really good one. Can we modify this script to know who deleted which snapshot. I feel it could be more helpful to track the snapshot deletions when many are using a single VM.

Thanks.

16 04 2013
TSR

This is really good one. Can we modify this script to know who deleted which snapshot. I feel it could be more helpful to track the snapshot deletions when many are using a single VM.

Thanks.

28 09 2013
patrick

21degrees from the start point

17 01 2014
George Sherman

thank you for making it is easy to impress the boss!

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




%d bloggers like this: