I was cleaning up some checkpoints in my home data-center lab today, and started tinkering with a script to automate the task. I put together a couple of short “script-lets” i can put in my task scheduler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
###################### #Using AD ###################### #Note: The user running the script must have at least read permissions in the AD where the #Hyper-V cluster "lives" and be a member of the "Hyper-V Administrators" group on each cluster node. #Import Modules ipmo ActiveDirectory ipmo Hyper-V #Get servers $hyperv = Get-ADObject -SearchBase "DC=domain,DC=com" -Filter 'ObjectClass -eq "serviceConnectionPoint" -and Name -eq "Microsoft Hyper-V"' $servers = $hyperv | ForEach-Object { $_.DistinguishedName.Split(",")[1].replace("CN=","") } #Remove old snapshots Get-VM -ComputerName $servers | Get-VMSnapshot | Where-Object {$_.CreationTime -lt (Get-Date).AddDays(-2) } | Remove-VMSnapshot |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
###################### #Using cluster ###################### #Note: Can be run from a computer/by a user in any domain or forrest that has two way trust #with the domain of the cluster. The user running the script must have at least read #permissions on the cluster and be a member of the "Hyper-V Administrators" group on each #cluster node. #Import Modules ipmo Hyper-V #Get servers $nodes = Get-ClusterNode -Cluster hpvclu.domain.com | Select -Expand Name #Remove old snapshots Get-VM -ComputerName $nodes | Get-VMSnapshot | Where-Object {$_.CreationTime -lt (Get-Date).AddDays(-2) } | Remove-VMSnapshot |