VMware Cloud Community
c-drive
Contributor
Contributor

Remote shutdown of ESX hosts via windows bat script

Hello all,

We had a heck of a time figuring out how best to shut down our ESX hosts (and each of the VMs) via our Liebert NFinity system, so I wanted to post this script to try and help some other folks out.

The shutdown flowchart goes like this: Libert NFinity -> Standalone windows server -> plink.exe (opens remote shell script) -> VMs shut down

In order to implement this script, first log in to your ESX host via ssh, making sure that you are logged in as a root user. Find a directory that suits you and open a new text file with vi or nano or something similar. The script is attached as shutdownGuests.sh. Make sure that it is chmod +x for the user that will be executing it via ssh. We added a user account on the ESX host to be used for shutdowns. This user account has the following line in /etc/sudoers:

<ESXUSER> ALL=NOPASSWD:/usr/bin/sudo, /bin/bash, /root/shutdownGuests.sh, /usr/bin/vmware-cmd, /bin/echo, /bin/date

On the windows side, you need to download plink.exe -- it's part of the Putty suite. You'll also need a .bat file to be called from your shutdown software.

I hope the scripts are pretty self-explanitory -- you want to change out the <USERNAME> instances for the username used to log on to the esx host, you want to change the rest as needed and we found it useful to install plink.exe in the path in Windows (c:/windows/system32). I'm sure that there are some better ways to do what we're doing here, but this should be a start :).

0 Kudos
10 Replies
espi3030
Expert
Expert

c-drive,

Nice work there! Here is a script I found online (can't remember where), I copied it to the /tmp/ directory on the ESX hosts and run it from there. The neat thing about this script is, it will attempt a soft shutdown (shutdown guest) and if that fails it will do a hard (power off) that is useful for vm's that don't have vmware tools installed. I did a chmod 700 on the script so it can only be executed by root. I will impliment your script as well.

0 Kudos
c-drive
Contributor
Contributor

That's great, thanks for the feedback. We'll definitely implement some of the features from your script as well -- it will be nice to shut down the guests no matter what after a timeout.

0 Kudos
champcf
Enthusiast
Enthusiast

Thanks for the script. I have 2 things I want to ask.

1. Seems like ESXi doesn't have /usr/bin/vmware-cmd. What might the ESXi version be?

2. What would the command be for suspend rather than shutdown?

Yes, I'm well aware that VMware doesn't want me doing anything by myself with the ESXi console (and especially over SSH).

0 Kudos
R2AD
Contributor
Contributor

0 Kudos
sfurness
Contributor
Contributor

The PowerOff.sh doesn't find the guest if there are spaces in the VM dir/file names. The default field separator is a space, so changing it to something else seemed to fix it.

Add to beginning of script

SAVEIFS=$IFS

IFS=$(echo -en "\n\b")

Add to end of script

IFS=$SAVEIFS

0 Kudos
Gooseblade
Contributor
Contributor

Using the script (20080808) from tooms.dk on ESX4 didn't work straight off ; I had to replace

vmware-cmd "$vm" getstate -q

with

vmware-cmd -q "$vm" getstate

Having done this, it appears to work fine.

0 Kudos
TLeach
Contributor
Contributor

Hi,

I didn't see a response to this query about ESXi hosts. Is there any way of implementing the script under ESXi?

Thanks,

Ted

0 Kudos
cdrivedave00
Contributor
Contributor

Try this version for ESXi 4.1:

<code>

#!/bin/sh

# read the list of VM ids

vim-cmd vmsvc/getallvms | grep vmx | awk '{print $1}' > /tmp/vm_ids.txt

# iterate over the list and shut down the VMs

while read line

do

     vim-cmd vmsvc/power.shutdown $line

done < /tmp/vm_ids.txt

rm /tmp/vm_ids.txt

</code>

You really dug this post up from a while ago Smiley Happy.

Cheers!

Dave

0 Kudos
hansis
Contributor
Contributor

hello

i know that this is an old post.

but can you tell me what right i have to give for an user "TEST" in group "users" that i can execute 

0 Kudos
hansis
Contributor
Contributor

hello

i know this is an old post. but i have problems with an shutdown-script. i have written a script with your instructions.

Now this user should be able to list and shutdown vms.

vmware-vim-cmd vmsvc/getallvms

But I get following error:

Failed to login: vim.fault.NoPermission

do you know that error?

see following post

thx.

hansi

0 Kudos