Created
October 8, 2023 00:49
-
-
Save rmorimoto4/d46a0e5d817facc9f8d801efd1e576db to your computer and use it in GitHub Desktop.
Shutdown of all VMs on the specified host and host
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Setting env | |
export GOVC_URL=<vCenter FQDN> | |
export GOVC_USERNAME=XXXXX | |
export GOVC_PASSWORD=XXXXX | |
export GOVC_INSECURE=true | |
# Add host names you want to shutdown | |
host_names=("ESXi FQDN#1" "ESXi FQDN#2" "ESXi FQDN#3" "ESXi FQDN#4") | |
for host_name in "${host_names[@]}"; do | |
h=$(govc find -i . -type h -name $host_name) | |
# Shutting down powered-on virtual machines | |
govc find . -type m -runtime.powerState poweredOn -runtime.host $h | xargs govc vm.power -s | |
if [ $(govc host.info -host.dns=$host_name -json | jq '.hostSystems[0].runtime.inMaintenanceMode') != true ] ; then | |
# Enter maintenance mode | |
govc host.maintenance.enter -host.dns=$host_name | |
# Shutdown Host | |
govc host.shutdown -host.dns=$host_name | |
fi | |
done | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment