Introduction to pyVmomi [Part-2]!!

In this follow up post, we will be looking at few more cmdlets that could come in handy when working with python and ESXi. Initially, when I had written the first blog in this series, I had not really planned for a second part to it. But, after playing around with pyVmomi for a while, I thought it was really worth a second post in the series.
1- Listing out VMs on a vCenter server instance:
#Connect to the vCenter server
vc = SmartConnect(host=vcenter, user=user, pwd=password, sslContext=context)
print(vc)
cont = vc.content
objview = cont.viewManager.CreateContainerView(cont.rootFolder, [vim.VirtualMachine], True)
vmuuid= objview.view
for vm in vmuuid:
print(vm.name)
Disconnect(vc)

2- Listing out VMs on an ESXi host:
h = SmartConnect(host=host, user=user, pwd=password, sslContext=context )
print(h)
cont = h.content
objview = cont.viewManager.CreateContainerView(cont.rootFolder, [vim.VirtualMachine], True)
vmuuid= objview.view
for vm in vmuuid:
print(vm.name)

3- Filter out a particular VM (on which you can perform other function/gather information):
h = SmartConnect(host=host, user=user, pwd=password, sslContext=context )
print(h)
cont = h.content
objview = cont.viewManager.CreateContainerView(cont.rootFolder, [vim.VirtualMachine], True)
vmuuid= objview.view
for vm in vmuuid:
if vm.name == 'nESXi1':
uuid= vm
print(uuid)

4- Another interesting feature of python, that can really help us write codes is the 'dir(obj)' cmdlet. This allows us to view all the attributes to an object:
h = SmartConnect(host=host, user=user, pwd=password, sslContext=context )
print(h)
cont = h.content
objview = cont.viewManager.CreateContainerView(cont.rootFolder, [vim.VirtualMachine], True)
vmuuid= objview.view
attrb= dir(vmuuid[0])
print(attrb)
Disconnect(h)

Let us look at some of the attributes of a VM that we can use:
1- Virtual Machine Summary:
h = SmartConnect(host=host, user=user, pwd=password, sslContext=context )
print(h)
cont = h.content
objview = cont.viewManager.CreateContainerView(cont.rootFolder, [vim.VirtualMachine], True)
vmuuid= objview.view
for vm in vmuuid:
if vm.name == 'nESXi1':
tgtvmuuid = vm
print(tgtvmuuid)
print(tgtvmuuid.summary)
Disconnect(h)

2- virtual machine config sumary:
h = SmartConnect(host=host, user=user, pwd=password, sslContext=context )
print(h)
cont = h.content
objview = cont.viewManager.CreateContainerView(cont.rootFolder, [vim.VirtualMachine], True)
vmuuid= objview.view
for vm in vmuuid:
if vm.name == 'nESXi1':
tgtvmuuid = vm
print(tgtvmuuid)

3- Virtual Machine power operations:
cont = h.content
objview = cont.viewManager.CreateContainerView(cont.rootFolder, [vim.VirtualMachine], True)
vmuuid= objview.view
for vm in vmuuid:
if vm.name == 'nESXi1':
tgtvmuuid = vm
print(tgtvmuuid)
tgtvmuuid.PowerOff()
Disconnect(h)

Refer the pyVmomi official documentation to understand more attributes and cmdlets.