b51444d Nov 24, 2017
1 contributor

Users who have contributed to this file

27 lines (20 sloc) 1.03 KB
# Description: Python sample to get VMs and its moid using vCenter server REST API.
# Reference:
# Make sure you have "" file into your python directory.
import vcrest
import json
vcip="" # vCenter server ip address/FQDN
#Get vCenter server session and can be used as needed. pass vcenter username & password
vcsession = vcrest.get_vc_session(vcip,"Administrator@vsphere.local","VMware1!")
#Get all the VMs from inventory using below method from "vcrest" module.
vms = vcrest.get_vms(vcip)
# Parsing the JSON response we got from above function call (it has all the Vms present in inventory
print "VM names and its unique MOID"
print "============================"
for vm in json_data:
print vm.get("name")+" :: "+vm.get("vm")
#We are powering on all the VMs those are in powered off state
if vm.get("power_state") == "POWERED_OFF":