-
-
Save jpluimers/44c54e680b5d2887e3a5 to your computer and use it in GitHub Desktop.
#!/bin/sh | |
# https://wiert.me/2021/04/30/vmware-esxi-console-viewing-all-vms-suspending-and-waking-them-up-part-5/ | |
RUNNING=0 | |
vmids=`vim-cmd vmsvc/getallvms | sed -n -E -e "s/^([[:digit:]]+)\s+((\S.+\S)?)\s+(\[\S+\])\s+(.+\.vmx)\s+(\S+)\s+(vmx-[[:digit:]]+)\s*?((\S.+)?)$/\1/p"` | |
for vmid in ${vmids} ; do | |
# echo "Probing VM with id: $vmid." | |
powerState=`vim-cmd vmsvc/power.getstate ${vmid} | sed '1d'` | |
name=`vim-cmd vmsvc/get.config ${vmid} | sed -n -E -e '/\(vim.vm.ConfigInfo\) \{/,/files = \(vim.vm.FileInfo\) \{/ s/^ +name = "(.*)",.*?/\1/p'` | |
vmPathName=`vim-cmd vmsvc/get.config ${vmid} | sed -n -E -e '/files = \(vim.vm.FileInfo\) \{/,/tools = \(vim.vm.ToolsConfigInfo\) \{/ s/^ +vmPathName = "(.*)",.*?/\1/p'` | |
# echo "VM with id ${vmid} has power state ${powerState} (name = ${name}; vmPathName = ${vmPathName})." | |
if [ "${powerState}" == "Powered on" ] ; then | |
RUNNING=1 | |
echo "Powered on VM with id ${vmid} and name: $name" | |
echo "Suspending VM with id ${vmid} and name: $name" | |
vim-cmd vmsvc/power.suspend ${vmid} > /dev/null & | |
fi | |
done | |
while true ; do | |
if [ $RUNNING -eq 0 ] ; then | |
echo "Gone..." | |
break | |
fi | |
RUNNING=0 | |
for vmid in ${vmids} ; do | |
# echo "Probing VM with id: $vmid." | |
powerState=`vim-cmd vmsvc/power.getstate ${vmid} | sed '1d'` | |
name=`vim-cmd vmsvc/get.config ${vmid} | sed -n -E -e '/\(vim.vm.ConfigInfo\) \{/,/files = \(vim.vm.FileInfo\) \{/ s/^ +name = "(.*)",.*?/\1/p'` | |
vmPathName=`vim-cmd vmsvc/get.config ${vmid} | sed -n -E -e '/files = \(vim.vm.FileInfo\) \{/,/tools = \(vim.vm.ToolsConfigInfo\) \{/ s/^ +vmPathName = "(.*)",.*?/\1/p'` | |
# echo "VM with id ${vmid} has power state ${powerState} (name = ${name}; vmPathName = ${vmPathName})." | |
if [ "${powerState}" == "Powered on" ] ; then | |
RUNNING=1 | |
echo "Waiting for id ${vmid} and name: $name..." | |
fi | |
done | |
sleep 1 | |
done | |
exit 0 |
Nice script; however in ESXi 6.5 I have a little trouble with line 3. I get empty lines, as well as the line "Default" so it gets a little messy later on when the $VM variable sometimes is not a valid VM id. I found it works better to set VMS
to the output of this:
vim-cmd vmsvc/getallvms | awk '{print $1}' | grep '[0-9]'
(I just understood why "Default" is printed - it is from a long VM comment text that wraps over several lines, so could be different in other circumstances).
Thanks! It reminds me that I forgot to update this gist after writing https://wiert.me/2021/04/30/vmware-esxi-console-viewing-all-vms-suspending-and-waking-them-up-part-5/ which now uses this to get all the VM IDs:
vmids=`vim-cmd vmsvc/getallvms | sed -n -E -e "s/^([[:digit:]]+)\s+((\S.+\S)?)\s+(\[\S+\])\s+(.+\.vmx)\s+(\S+)\s+(vmx-[[:digit:]]+)\s*?((\S.+)?)$/\1/p"`
It still has the drawback that you can get an invalid VM ID if the wrapped VM comment contains a number as the first word on a line.
There seems to be nothing that can prevent that.
I will commit a new version here shortly.
Great, this works fine too!
Great, this works fine too!
Glad it does. Always happy when things are also useful to others (:
@jpluimers Thanks for making this. I couldn't get it to work however, and tracked it to that the initial sed
on getallvms
didn't match anything.
It turns out that I have spaces in all my volume names, which this doesn't expect. I replaced (\[\S+\])
with (\[[^]]+\])
and now it works fine also when there's spaces in the volume names.
Via: https://thisiscasperslife.wordpress.com/2011/12/04/wmware-esxi-suspend-all-guests/
Fixed some typos and added some extra logging.