Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/sh
VMS=`vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}'`
for VM in $VMS ; do
# echo "Probing VM with id: $VM."
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
name=`vim-cmd vmsvc/get.config $VM | grep -i "name =" | awk '{print $3}' | head -1 | awk -F'"' '{print $2}'`
echo "VM with id $VM has power state $PWR (name = $name)."
done
@PeterMosmans
Copy link

PeterMosmans commented Aug 3, 2015

Hope you don't mind my comment (I'm a fan of awk):
You can write

VMS=`vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}'`

as

VMS=$(vim-cmd vmsvc/getallvms | awk '!/Vmid/{print $1}')

which saves a subprocess (grep)

@NiteCrwlr
Copy link

NiteCrwlr commented Sep 25, 2019

Hi! Thanks for your script!
If you have comments in yor VM's, the script will output some errors like:

(vim.fault.NotFound) {
faultCause = (vmodl.MethodFault) null,
faultMessage =
msg = "Unable to find a VM corresponding to "something in the comment"
}

To keep this nice and beautiful, I added a regex check for numeric values in the for-loop:

#!/bin/sh
VMS=vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}'
for VM in $VMS ; do
if echo $VM | grep -e '^[0-9]' >/dev/null; then
# echo "Probing VM with id: $VM."
PWR=vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"
name=vim-cmd vmsvc/get.config $VM | grep -i "name =" | awk '{print $3}' | head -1 | awk -F'"' '{print $2}'
echo "VM with id $VM has power state $PWR (name = $name)."
fi
done

@zengyijian916
Copy link

zengyijian916 commented Jun 22, 2021

Hi, can you update for just show the power on VMs, not all. Many Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment