Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

Automated shutdown of ESXi and VMs through Powershell

1 min read

My friend was running a cybercafe center. He has dozens of ESXi running 100 of VMs. He was using Dell Multi-UPS Management console to handle his small computer center. The very often power cut was ruining his business. He called up me to see there is a possible solution to handle graceful shutdown of VMs and then ESXi through the available tools and technique. This solution really helped us to handle the situation gracefully.

I just noted down the steps roughly and will not be late to share it with you:

1. Download the Dell Multi-UPS Management Console from http://www.dell.com/support/home/us/en/19/Drivers/DriversDetails?driverId=R318771

2. Execute DELL_MULTI-DEVICE_A00_R318771.exe and run the setup to install on Windows 2008 R2.

3. Once installed, try accessing it through:

http://127.0.0.1:4679/default.html

4. The default credential for the MUMC login is admin/admin

5.A quick scan is automatically performed and you will get to see the overall power devices available.

6. Click on Settings > Systems and enable shutdown as shown below

7. Click on Shutdown option and configure as shown below

The above shown is a sample entry. Change it as per your required IPs.

In our case, that’s :

Power Source: 192.168.1.99

Load Segment: Master Output

Login/password: admin/admin

Shutdown timer : 180 sec

Shutdown duration: 120 sec

Type: Script

Location: C:\scripts/Ajeet.bat

8. Save the settings

9. Now let’s create a required scripts one by one for shutting down ESXi and VMs.

Script 1:

location: C:\scripts\Ajeet.bat

Start shutdownvms.bat

Script 2:

location: C:\scripts\shutdownvms.bat

———- Start——–

PowerShell.exe C:\Scripts\shutdownallvms.ps1

———End————–

Script 3:

Location:C:\scripts\shutdownallvms.ps1

========= START =========

Add-Snappin “Vmware. Automation.Core”

Connect-VIServer IPADDRESS

# Get All the ESX Hosts

$ESXSRV = Get-VMHost

# For each of the VMs on the ESX hosts

Foreach ($VM in ($ESXSRV | Get-VM)){

# Shutdown the guest cleanly

$VM | Shutdown-VMGuest -Confirm:$false

}

# Set the amount of time to wait before assuming the remaining powered on guests are stuck

$waittime = 200 #Seconds

$Time = (Get-Date).TimeofDay

do {

# Wait for the VMs to be Shutdown cleanly

sleep 1.0

$timeleft = $waittime – ($Newtime.seconds)

$numvms = ($ESXSRV | Get-VM | Where { $_.PowerState -eq “poweredOn” }).Count

Write “Waiting for shutdown of $numvms VMs or until $timeleft seconds”

$Newtime = (Get-Date).TimeofDay – $Time

} until ((@($ESXSRV | Get-VM | Where { $_.PowerState -eq “poweredOn” }).Count) -eq 0 -or ($Newtime).Seconds -ge $waittime)

# Shutdown the ESX Hosts

$ESXSRV | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)}

Write-Host “Shutdown Complete”

===== END ========

10. Now its time to test drive the scripts. Just try running those script manually and confirm if those works or not.

11. Now time to test drive with UPS System menu option. Click on Shutdown option of MUMC and Click on Test Shutdown. The Script should work and power-on VMs and ESXi gradually.

12. Try creating the power-off situation and let UPS perform this activity of running the scripts to shutdown ESXi and VMs.

It really went flawless saving couple of rupees for my friend. He treated me the next day for the overall efforts and we rocked the party too 🙂

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server