-
Notifications
You must be signed in to change notification settings - Fork 18
/
esxi-stop-vm.sh
35 lines (26 loc) · 1.04 KB
/
esxi-stop-vm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/sh
################################################################################
#
# Command-line parameters: user_id esxi_host_name vmx_base_filename
#
# Stop guest virtual machine with vmx file (vmx_base_filename'.vmx') on remote
# VMware ESXi server (esxi_host_name) using given user credentials (user_id)
#
################################################################################
# Check for invocation errors
if [ $# -ne 3 ]; then
echo "$0: error! Not enough arguments"
echo "Usage is: $0 user_id esxi_host_name vmx_filename"
echo "Only specify the vmx basefilename; leave off the '.vmx' extension"
exit 1
fi
. /mnt/tank/systems/scripts/esxi.config
# Gather command-line arguments for user ID, hostname, and datastore name:
esxiuser=$1
esxihost=$2
vmxname=$3
guestvmids=$(ssh "${esxiuser}"@"${esxihost}" vim-cmd vmsvc/getallvms | grep "/${vmxname}.vmx" | awk '$1 ~ /^[0-9]+$/ {print $1}')
echo "$(date): $0 ${esxiuser}@${esxihost} vmx=${vmxname}.vmx"
for guestvmid in $guestvmids; do
shutdown_guest_vm "$guestvmid"
done