Skip to content

Instantly share code, notes, and snippets.

@hartsock
Last active March 16, 2022 23:04
Show Gist options
  • Save hartsock/429e8f7d539ffc62d012 to your computer and use it in GitHub Desktop.
Save hartsock/429e8f7d539ffc62d012 to your computer and use it in GitHub Desktop.
A quick and dirty script to list all the vms attached to a network ... by datacenter.
from __future__ import print_function
from pyVim import connect
def list_all_networks_and_vms():
si = connect.SmartConnect(host='vcsa',
user='root',
pwd='my_password')
root_folder = si.content.rootFolder
datacenters = [entity for entity in root_folder.childEntity
if hasattr(entity, 'networkFolder')]
for datacenter in datacenters:
for network in datacenter.networkFolder.childEntity:
if hasattr(network, 'vm'):
print('datacenter: ', datacenter.name,
' network: ', network.name)
for vm in network.vm:
print(' ', vm.name)
elif hasattr(network, 'summary'):
print('datacenter: ', datacenter.name,
'dvs: ', network.uuid)
for vm in network.summary.vm:
print(' ', vm.name)
elif hasattr(network, 'portKeys'):
print('datacenter: ', datacenter.name,
'portgroup: ', network.key)
port_dvs = network.config.distributedVirtualSwitch
for vm in port_dvs.summary.vm:
print(' ', vm.name)
list_all_networks_and_vms()
@hartsock
Copy link
Author

TODO: develop into a full sample per: vmware/pyvmomi-community-samples#138

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