vmware/pyvmomi-community-samples

Rescan using Pyvmomi

Open

#475 opened on Mar 30, 2018

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Python (972 stars) (914 forks)batch import
help wanted

Description

Hi ,

I have a requirement to rescan newly added disks to vsphere ... From the samples I have written the following code to get the lun id attached to the VM. From little research I understood that Managed Object - HostStorageSystem is the object I need to use, But I'm not sure how to use it in my code ,can anyone please help me here

import atexit from pyVim import connect from pyVmomi import vmodl from pyVmomi import vim

host = "vgervc.lss.emc.com" user = "vcuser@vsphere.local" password = "remember" port = 443

If you use self signed certificates Python will not connect.

This will give you an error for the check, but continues.

import ssl

import ssl try: _create_unverified_https_context = ssl._create_unverified_context except AttributeError: # Legacy Python that doesn't verify HTTPS certificates by default pass else: # Handle target environment that doesn't support HTTPS verification print ("There was an error with the SSL certificates. Continuing..") ssl._create_default_https_context = _create_unverified_https_context

def main(): try: service_instance = connect.SmartConnect(host=host, user=user, pwd=password, port=port)

    # What to do when exiting
    atexit.register(connect.Disconnect, service_instance)
    content = service_instance.RetrieveContent()
    container = content.rootFolder  # starting point to look into
    viewType = [vim.VirtualMachine]  # object types to look for
    recursive = True  # whether we should look into it recursively
                 # Create a view
    containerView = content.viewManager.CreateContainerView(
        container, viewType, recursive)

    # Loop through all obhects to return name and VMware tools version
    children = containerView.view
    # for child in children:
    #     if child.summary.guest is not None:
    #         try:
    #             tools_version = child.summary.guest.toolsStatus
    #             print("VM: {}, VMware-tools: {}".format(child.name, tools_version))
    #         except:
    #              print("Vmware-tools: None")



except vmodl.MethodFault as error:
    print("Caught vmodl fault : " + error.msg)
    return -1
vm_name="dlqa4159_Neel"
content = service_instance.RetrieveContent()
vm = get_obj(content, [vim.VirtualMachine], vm_name)
for dev in vm.config.hardware.device:
    if isinstance(dev, vim.vm.device.VirtualDisk):
                print("Entered the Virtual Disk code")
                print((dev))
                exit(0)
return 0

def get_obj(content, vimtype, name): obj = None container = content.viewManager.CreateContainerView( content.rootFolder, vimtype, True) for c in container.view: if c.name == name: obj = c break return obj

Start program

if name == "main": main()

Contributor guide