Skip to content

Instantly share code, notes, and snippets.

@peterhynes
Created February 7, 2018 15:59
Show Gist options
  • Select an option

  • Save peterhynes/8661604255f8a235f8a05e29d481b7db to your computer and use it in GitHub Desktop.

Select an option

Save peterhynes/8661604255f8a235f8a05e29d481b7db to your computer and use it in GitHub Desktop.
#Python script to test connection to vcenter api and pull vm information
from pyVim.connect import SmartConnect, Disconnect
import ssl
username = input("Please enter your username: ")
password = input("Please enter your password: ")
s =ssl.SSLContext(ssl.PROTOCOL_TLSv1_1)
s.verify_mode = ssl.CERT_NONE
try:
c = SmartConnect(host="VcenterIP", user=username, pwd=password)
print("Valid Certificate")
except:
c = SmartConnect(host="VcenterIP", user=username, pwd=password, sslContext=s)
print("Invalid or Untrusted Certificate")
print("")
datacenter = c.content.rootFolder.childEntity[0]
vms = datacenter.vmFolder.childEntity
for i in vms:
print("Virtual Machine Name: "+i.name)
if i.guest is not None and i.guest.guestFullName is not None:
print("Virtual Machine OS : "+i.guest.guestFullName)
else:
print("Virtual Machine OS : ")
print("")
Disconnect(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment