Last active
August 29, 2015 14:21
-
-
Save danhigham/70e87ff5281f977d4c01 to your computer and use it in GitHub Desktop.
Shutdown and destroy all VMs in a resource pool
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
| #!/usr/bin/env ruby | |
| require 'rbvmomi' | |
| vcenter_host = ARGV[0] | |
| username = ARGV[1] | |
| password = ARGV[2] | |
| dc_name = ARGV[3] | |
| rp_name = ARGV[4] | |
| def vms(folder, resource_pool) # recursively go thru a folder, dumping vm info | |
| folder.childEntity.each do |x| | |
| ent_class = x.class | |
| vms x, resource_pool if x.class == RbVmomi::VIM::Folder | |
| if x.class == RbVmomi::VIM::VirtualMachine | |
| next if x.resourcePool.nil? | |
| if x.resourcePool.name == resource_pool && (x.name[0..1] == "vm" || x.name[0..1] == "sc") | |
| puts "Shutting down #{x.name}" | |
| begin | |
| x.PowerOffVM_Task.wait_for_completion | |
| rescue RbVmomi::Fault # maybe the vm is already shutdown | |
| end | |
| x.Destroy_Task.wait_for_completion | |
| end | |
| end | |
| end | |
| end | |
| vim = RbVmomi::VIM.connect host: vcenter_host, user: username, password: password, insecure: true | |
| rootFolder = vim.serviceInstance.content.rootFolder | |
| dc = vim.serviceInstance.find_datacenter(dc_name) | |
| vms dc.vmFolder, rp_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment