Skip to content

Instantly share code, notes, and snippets.

@hartsock
Created December 4, 2014 00:23
Show Gist options
  • Save hartsock/af6d92c77367a9d5d108 to your computer and use it in GitHub Desktop.
Save hartsock/af6d92c77367a9d5d108 to your computer and use it in GitHub Desktop.
Find a virtual machine by it's datastore path
si = connect.SmartConnect(host='vcsa', user='my_user', pwd='my_password')
content = si.RetrieveContent()
datacenters = [entity for entity in content.rootFolder.childEntity
if hasattr(entity, 'vmFolder')]
dc = None
for datacenter in datacenters:
if datacenter.name == 'my_datacenter':
dc = datacenter
search = content.searchIndex
vm = search.FindByDatastorePath(dc, '[my_datastore_name] my_vm/my_vm.vmx')
# proof:
print(vm)
@hartsock
Copy link
Author

hartsock commented Dec 4, 2014

NOTE: you will need to know,

  • the name of your VM (and possibly its VMX file if the VM and the VMX aren't the same name)
  • the name of its datastore
  • the name of the datacenter

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