Here's a light one to end the week in a good mood! Expect no complex engineering though, PowerCLI scripts to manage snapshots must be very common around!
I manage a small environment where uber-scripting skills are not required. But for the sake of efficiency, laziness and "intellectual effort", here's what I use to patch my machines. All my VMs have custom attributes which include among others Business Owner, Primary IT Administrator, Emergency contact and an obscure one called "IsMirroredToDR". For historical reasons we have a software called HP Storage Mirroring for Virtual Infrastructure (SMVI) which works with ESX 4 (not ESXi), it does a great job but sometimes is a pain when things go wrong. This program creates snapshots and replicates them from production to DR sites at regular intervals for business recovery/continuity purposes.
Now whenever I need to patch servers I of course create snapshots in case something would go wrong. I however need to take special care of the VMs which have the IsMirroredToDR custom attribute set to True. I therefore use the script below to automatically create a snapshot on all VMs that are not mirrored to our DR site, and filter out VMs that are being mirrored so I can take care of those later. Once I have properly stopped all the mirroring processes and ensured all snapshots for these mirrored VMs have been deleted (as in consolidated to the base VMDKs), I then change the "False" to "True", run the script again et voila, even mirrored VMs are snapshotted 🙂
I had also tested a version where I also filtered VMs per datacenter, but I somehow didn't save it.
I hope this code snippet will be useful to someone, though it's definitely not rocket science.
Have a nice weekend!
#Get a list of VMs who have the IsMirroredToDR custom attribute set to false, then sort ascending
$VMs = @(Get-VM|Get-Annotation -CustomAttribute "IsMirroredToDR" | Where {$_.Value -eq "False"} | Sort-Object AnnotatedEntity)
#begin action on each filtered VM (comment/uncomment as appropriate)
ForEach ($VM in $VMs) {
#****CREATE SNAPSHOTS****
#New-Snapshot -VM $VM.AnnotatedEntity -Name BeforePatch
}