use get-vmsnapshot & remove-vmsnapshot for older snapshots

Oct 14, 2010 at 3:51 AM


I'm trying to create a combined PS script that will complete the following:

Ping a specific VM to ensure its currently on this host (clustered Hyper-V)

If the VM is on this host, delete snapshots older than 2 weeks (we take weekly snapshots), then Perform a VM-Shutdown, Wait for 1 hour, then start it back up (so the disks can merge)


The above script is working, except I haven't figured out how to get a list of snapshots, then delete ones older than 2 weeks.


If you could help that would be great!

If you need any more info let me know.



Script Sample:

##This Script Imports the Hyper-V Powershell Modules, then initiates a VM Shutdown, waits, then starts the VM back up

##Imports the Hyper-V Modules

Import-Module 'C:\Program Files\modules\Hyperv\Hyperv.psd1'

## Checks if VM exists on this node, then shuts it down, waits then starts it back up

$Result = ping-vm -vm 'VMNAME'

if($Result.Status -eq 'Success')
	Invoke-VMShutdown -VM 'VMNAME' -Server 'HOSTNAME' -Force
	Start-VM -VM 'VMNAME'