Skip to content

Instantly share code, notes, and snippets.

@sdorsett
Created February 10, 2017 21:23
Show Gist options
  • Save sdorsett/904a7e91a04c8e7a7f8590366ef9ed1a to your computer and use it in GitHub Desktop.
Save sdorsett/904a7e91a04c8e7a7f8590366ef9ed1a to your computer and use it in GitHub Desktop.
rbvmomi - find all virtual machines with 'desktop' in the name
require 'rbvmomi'
hostname = '192.168.1.200';
username = 'user1@vsphere.local';
password = 'P@ssword123';
vim = RbVmomi::VIM.connect host: hostname, user: username, password: password, insecure: true;
rootFolder = vim.serviceInstance.content.rootFolder
viewManager = vim.serviceInstance.content.viewManager
viewManager.CreateContainerView(
recursive: true, container: rootFolder, type: ['Datacenter']
).view.each{|dc|
viewManager.CreateContainerView(
recursive: true, container: dc, type: ['ComputeResource']
).view.each{|cluster|
viewManager.CreateContainerView(
recursive: true, container: cluster, type: ['VirtualMachine']
).view.
select{|vm|
vm.name =~ /desktop/
}.
each{|vm|
puts "in datacenter #{dc.name}, cluster #{cluster.name}: vm #{vm.name}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment