Script: Emergency

Posted by : on

Category : esx   script   vmware


I was asked to write a script that I could run locally on an ESX server that would scan the state of the VMs and then try to gracefully power off the VMs that were running. After the VMs are powered off, it would then power off the ESX server.

The reason the script was needed was because I had to do maintenance on the ESX Server and I needed a more automated way to bring down the environment. In this particular environment, each ESX server had locally stored VMs.

This script was written for an ESX 3.5 environment. Graceful shutdowns will require VMware Tools installed on each VM.

#!/bin/bash
# emergencyshutdown.sh
# Written By Sam Aaron 

while :
do
     clear
     echo  "****************************************************";
     echo "*** This is an EMERGENCY STOP of the ESX System. ***";
     echo  "****************************************************";
     echo "1. Power off all VMs and the ESX System";
     echo "2. Exit";
     echo -n "Please enter option [1 or 2]";
     read opt
     case $opt in
     1) echo "************************************";
     echo "*** All VMs will be powered off. ***";
     echo "************************************";

     for vm in `vmware-cmd -l`;
          do
               vmx="`basename ${vm}`";
               vmware-cmd "$vm" stop trysoft;
               echo "This ${vm} has been powered off.";
          done;

     echo  "************************************************";
     echo "*** Verifying all VMs have been powered off. ***";
     echo  "************************************************";

     for vm in `vmware-cmd -l`;
          do
               vmx="`basename ${vm}`";
               state="`vmware-cmd -q ${vm} getstate`";
               echo "VM: ${vmx} is currently ${state}.";
          done;

     echo "************************************";
     echo "*** Powering off the ESX Server. ***";
     echo "************************************";

     shutdown -h now;
     exit $?;;

     2) echo "$USER has terminated the EMERGENCY STOP program.";
     exit 1;;
     *) echo "$opt is an invalid option. Please select option 1 or 2 only.";
     echo "Press [enter] key to continue. . .";
     read enterKey;;

     esac
done

I have revamped and rewritten this script.


About Sam Aaron

Father. Husband. Geek. Workaholic. US Marine Corps Veteran.

Sam Aaron is a Senior Consultant in the Professional Services Organization for Entelligence, bringing over a decade of expertise in enterprise cloud automation and infrastructure. Sam has spent almost eleven years at VMware leading cloud automation initiatives using VCF Automation (formerly Aria Automation & vRA) and designing scalable, multi-tenant environments with VMware Cloud Director (vCD).

Sam holds multiple certifications including VCF-Architect 2024, VCIX-CMA, and dual VCPs (DCV & CMA), and is a recognized contributor to VMware’s certification exams. As a VMware Hands-On Lab (HOL) Captain and content author from 2015-2025, Sam played a key role in educating and mentoring the global VMware community. He helped to create and develop the automation challenge and troubleshooting labs for VMworld and global virtual forums.

When Sam is not working, he has several hobbies, among these are 3D printing Star Wars robots and turning them into animatronics.

Launched in April 2010, micronauts is Sam's online presence. Here, he has been blogging and sharing knowledge with the virtualization community. This blog acts as a central repository to retain the resolutions and other trivial knowledge that Sam has discovered.

** No information provided here was reviewed or endorsed by VMware by Broadcom, Microsoft, or anyone else for that matter. All information here are opinions based on Sam's personal experience. Use this knowledge at your own risk. **

Star
Useful Links