VMware Cloud Community
Sanjuro
Contributor
Contributor

Emergency Startup and Shutdown of Prioritized VM's

All,

I'm looking for a way to priortize a startup and shutdown of VM's and ESXi 4.1 hosts in the event of a datacenter outage, leaving the host that is running my vCenter vm powered on. I want to be able to shut the guests down gracefully in an order of my choosing, as well as power them back up (after powering the hosts on manually). I'm looking to use the annotations field in the vm's summary tab as a piece to determine the priority of their shutdown and startup. So, for example, If I priortize my vm's by Group 1; Group 2; Group 3 and have the Script scan the annotations field of the vm's determine which group to shutdown first and which to shutdown last and vise versa for startups.

If you have a simpler idea of how to do this then please advise!

Thanks again for your expertise and assistance!

0 Kudos
3 Replies
LucD
Leadership
Leadership

That should work.

The only flaw I can see is that the custom attributes are only available through the vCenter.

So when the vCenter is down, you have no way of getting the order.

An alternative could be to use the Notes field, that is available when connected to the vCenter or any of the ESXi servers.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sanjuro
Contributor
Contributor

The notes field is what I was referring to under the Annotations section on the summary page. Thanks!

0 Kudos
cabraun
Enthusiast
Enthusiast

I did this a few years ago using the same method.  Essentially a note was added to all VM's establishing a ShutdownPriority of 1-9.  I also created folders and each VM was put, by the script, into its correct folder based on shutdown priority if it was not already in the correct folder for one reason or another.  Then the script shuts down all running VMs in each folder in order with a 60 second pause between folders.  Once all VMs are down, then vCenter is shutdown manually.

Hopefully you find this useful in at least getting started in your quest.

#Choose Source vCenter
$sourceVI = Read-Host "***USE WITH EXTREME CAUTION***!This script WILL shutdown all running VMs!***USE WITH EXTREME CAUTION*** To confirm your intent to shutdown all running VMs, Please type "srv-vcenter";
#Enter Credentials
$creds = get-credential
#Connect to Source vCenter
connect-viserver -server $sourceVI -credential $creds
#$vmlist = get-datacenter $datacenter -Server $sourceVI| get-vm
$folder1 = get-folder "ShutdownPriority1"
get-vm | where {$_.notes -like '*ShutdownPriority1*'} | move-vm -destination $folder1
$folder2 = get-folder "ShutdownPriority2"
get-vm | where {$_.notes -like '*ShutdownPriority2*'} | move-vm -destination $folder2
$folder3 = get-folder "ShutdownPriority3"
get-vm | where {$_.notes -like '*ShutdownPriority3*'} | move-vm -destination $folder3
$folder4 = get-folder "ShutdownPriority4"
get-vm | where {$_.notes -like '*ShutdownPriority4*'} | move-vm -destination $folder4
$folder5 = get-folder "ShutdownPriority5"
get-vm | where {$_.notes -like '*ShutdownPriority5*'} | move-vm -destination $folder5
$folder6 = get-folder "ShutdownPriority6"
get-vm | where {$_.notes -like '*ShutdownPriority6*'} | move-vm -destination $folder6
$folder7 = get-folder "ShutdownPriority7"
get-vm | where {$_.notes -like '*ShutdownPriority7*'} | move-vm -destination $folder7
$folder8 = get-folder "ShutdownPriority8"
get-vm | where {$_.notes -like '*ShutdownPriority8*'} | move-vm -destination $folder8
$folder9 = get-folder "ShutdownPriority9"
get-vm | where {$_.notes -like '*ShutdownPriority9*'} | move-vm -destination $folder9
get-folder $folder1 | get-vm | where {$_.Powerstate -eq "PoweredOn"} | shutdown-vmguest -confirm:$false
start-sleep 60
get-folder $folder2 | get-vm | where {$_.Powerstate -eq "PoweredOn"} | shutdown-vmguest -confirm:$false
start-sleep 60
get-folder $folder3 | get-vm | where {$_.Powerstate -eq "PoweredOn"} | shutdown-vmguest -confirm:$false
start-sleep 60
get-folder $folder4 | get-vm | where {$_.Powerstate -eq "PoweredOn"} | shutdown-vmguest -confirm:$false
start-sleep 60
get-folder $folder5 | get-vm | where {$_.Powerstate -eq "PoweredOn"} | shutdown-vmguest -confirm:$false
start-sleep 60
get-folder $folder6 | get-vm | where {$_.Powerstate -eq "PoweredOn"} | shutdown-vmguest -confirm:$false
start-sleep 60
get-folder $folder7 | get-vm | where {$_.Powerstate -eq "PoweredOn"} | shutdown-vmguest -confirm:$false
start-sleep 60
get-folder $folder8 | get-vm | where {$_.Powerstate -eq "PoweredOn"} | shutdown-vmguest -confirm:$false
start-sleep 60
get-folder $folder9 | get-vm | where {$_.Powerstate -eq "PoweredOn"} | shutdown-vmguest -confirm:$false
start-sleep 60
0 Kudos