Obtaining information for more than 4000 VMs via vSphere REST API
search cancel

Obtaining information for more than 4000 VMs via vSphere REST API

book

Article ID: 301570

calendar_today

Updated On:

Products

VMware vSphere ESXi

Issue/Introduction

To provide an alternate method to retrieve the list of VMs when the total count in the environment is over 4000.

Symptoms:
When using the 'GET https://{api_host}/api/vcenter/vm' REST API call to obtain a list of VMs, the retrieval is limited to a maximum of 4000 VMs at a time. Trying to fetch any more than 4000 VMs will lead to failure with a 500 error.

Environment

VMware vSphere ESXi 7.0

Resolution

The way around the 4000 limit is essentially to split the API calls to a per-Datacenter or per-Cluster or per-Host basis, depending on the number of VMs at each stage.

For example, if it is known that each Datacenter in the environment has less than 4000 VMs, then the following steps can be followed:

First list all the datacenters.
curl -X GET 'https://{VC}/api/vcenter/datacenter' -H 'vmware-api-session-id: ...'

Let's say the above request returns 3 Datacenters:
[
  {
    "name": "vAPISdkDatacenter",
    "datacenter": "datacenter-3"
  },
  {
    "name": "Sample_DC_1",
    "datacenter": "datacenter-31"
  },
  {
    "name": "Sample_DC_2",
    "datacenter": "datacenter-36"
  }
]

Next, make a total of 3 requests, one for each datacenter, to obtain the list of VMs from each one:
curl -X GET 'https://{VC}/api/vcenter/vm?datacenters=datacenter-3' -H 'vmware-api-session-id: ...'
curl -X GET 'https://{VC}/api/vcenter/vm?datacenters=datacenter-31' -H 'vmware-api-session-id: ...'
curl -X GET 'https://{VC}/api/vcenter/vm?datacenters=datacenter-36' -H 'vmware-api-session-id: ...'

Once done, simply merge the results of all 3 calls to obtain the complete list of VMs.
While the above example makes use of Datacenter as the division of choice, the same process can also be done with Clusters or Hosts instead, in case the number of VMs is even larger.

Additional Information

This article references the following documentation for the List VM REST API call: https://developer.vmware.com/apis/vsphere-automation/latest/vcenter/api/vcenter/vm/get/