Last active
March 16, 2022 23:04
-
-
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.
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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: develop into a full sample per: vmware/pyvmomi-community-samples#138