VMware Cloud Community
ajaypai1971
Contributor
Contributor

fndByDns is not finding some VMs

fndByDns is not finding some VMs.

im trying to write a script to list the properties of VMs via pymomi.

observed that it does not work for all VMs in my environment.

nslookup for the failing VMs are working, forward and reverse.

any guidance?

"""
import csv

from pyVim.connect import SmartConnect

import ssl

"""
function to convert VM names from csv to a list.
"""
def csv2list(csv_file😞

   vm_list = []

   with open('vm_names.csv', 'r') as f:

   # for row in csv.reader(f):
  # data.append(row)
   vm_list = [row for row in csv.reader(f)] # using list comprehension instead of code above
   print(vm_list)

   return(vm_list)

def vm_list(vm_properties):

  s = ssl.SSLContext(ssl.PROTOCOL_SSLv23)  # For VC 6.5/6.0 s=ssl.SSLContext(ssl.PROTOCOL_TLSv1)
   s.verify_mode = ssl.CERT_NONE

  vc_fqdn = "<vc>"
   user_name = "<user>"
   password = "<password>"

   vc_connect = SmartConnect(host=vc_fqdn, user=user_name, pwd=password, sslContext=s)

   vm = None

   search_index = vc_connect.content.searchIndex



   for v in vm_properties:

   try:

  vm = search_index.FindByDnsName(dnsName = str(v[0]), vmSearch = True)

   #str(v[0]) since csv2list retunrs list of lists. like [[vm0],[vm1]]

  #vmSearch is for searching vms,else search forESXi hosts

   details = {

   'name': vm.summary.config.name,

   'instance UUID': vm.summary.config.instanceUuid,

   'bios UUID': vm.summary.config.uuid,

   }

   for name,value in details.items():

   print(u" {0:{width}{base}}: {1}".format(name, value, width=25, base='s'))

   print("\n\n\n")

   except AttributeError as e:

   print("vm {} has ".format(str(v[0]))+ str(e))

"""
MAIN program
"""



csvfile = "vm_names.csv"

vm_properties = csv2list(csvfile)

vm_list(vm_properties)

0 Kudos
2 Replies
daphnissov
Immortal
Immortal

You might want to put this in the VMware {code} forum as this one is for vRealize Automation.

0 Kudos
dtaliafe
Hot Shot
Hot Shot

Do the VMs that are not being found have VMware Tools running?  findByDNS is looking for the hostname in the GuestInfo.

https://www.vmware.com/support/orchestrator/doc/vro-vsphere60-api/html/VcGuestInfo.html#hostName

If that isn't set it's most likely an issue with VMware Tools.

0 Kudos