-- Ben Armstrong, Virtualization Program Manager
Talking about core virtualization at Microsoft (Hyper-V, Virtual PC and Virtual Server).
After the relatively trick “taking a snapshot” script – today’s snapshot script is quite easy. Listing the snapshots that a virtual machine has:
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
# Prompt for the virtual machine to use
$VMName = Read-Host "Specify the name of the virtual machine"
# Get the management service
$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
# Get snapshot objects associated with the virtual machine
$Snapshots = gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"
write-host
write-host $VMName "has the following snapshots:"
# Display a formatted list of snapshots
$Snapshots | sort CreationTime | Format-Table -Property @{n='Snapshot Name';e={$_.ElementName}}
One thing to point out here is that I am using a WMI query that looks at the association class (MSVM_ElementSettingData) as well as the result class (MSVM_VirtualSystemSettingData). The reason why I do this is that if you grab all MSVM_VirtualSystemSettingData classes that are associated with a virtual machine – you will get two extra entries: one for the active virtual machine, and one for the latest snapshot (which results in one of your snapshots being duplicated). The method that I am using here ensures that you only get snapshots, and you do not get any duplicates.
Cheers, Ben
This is nice, but I'm looking for something to list the Snapshot Name and its GUID. Can this be done via this method?