python-2.7vmwarepyvmomipyvimeo

How to find vm in particular folder using pyvmomi


Hi I am new in python and I am exploring pyvmomi. Here I want to fetch vm info.Like I have one data center i.e "DataCenter1" In that data center there are two folders LinuxServer and WindowsServer these folder contains vms.So I want to fetch vm name with their respective folder names

    DataCenter1
    |
    |----LinuxServer
    |    |---RHEL-VM
    |    |---Ubuntu-VM
    |
    |----WindowsServer
    |    |---win2k12r2-VM
    |    |---win2k8r2-VM

My code:

    from pyvim.connect import SmartConnect, Disconnect
    import ssl

    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    context.verify_mode = ssl.CERT_NONE
    connect = SmartConnect(host="172.0.0.0",user="root",pwd="****",port=int("443"),sslContext=context)

    datacenter = connect.content.rootFolder.childEntity[0]
    print (datacenter)
    vms = datacenter.vmFolder.childEntity
    for i in vms:
        print(i.name)
        #Here I want to fetch vm name and their respective folder names
    Disconnect(c)

Here I am able to fetch all vm names but I want to fetch folder name of respective vm. Is there any method ? Can you please guide me.


Solution

  • Here you will get parent name of that vm means i.e your folder name if it exist.

    from pyvim.connect import SmartConnect, Disconnect
    import ssl
    
    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    context.verify_mode = ssl.CERT_NONE
    connect = SmartConnect(host="172.0.0.0",user="root",pwd="****",port=int("443"),sslContext=context)
    
    datacenter = connect.content.rootFolder.childEntity[0]
    print (datacenter)
    vms = datacenter.vmFolder.childEntity
    for vm in vms:
        print(vm.parent.name)
    Disconnect(c)