I was writing a quick PowerShell script this morning and I wanted to get all of the VMs in Prism Central via the v3 API calls. I was struggling with the syntax until I found this post:
v3 - vms/list - Host to get full list of VMs?
The post lead me to this article:
The Five Hundred (500+ VMs via v3 API)
The article lead me to this code sample:
After reading those articles a few times I still didn’t understand what I needed to do...I finally figured it out. I could piece together the v3 API call, but I couldn’t get the correct number of VMs to return. After hitting the wall a few times I figured out the part I didn’t understand: the relationship between length and offset.
Here is what you need to understand:
- The “api/nutanix/v3/vms/list” v3 API command method is POST. That means you must include “body” in your API call
- To get the total number of VMs you can use this JSON:
{
"kind":"vm"
}Note: This will give you the total number of VMs in the cluster/PC. The results will look like this:
- To get the first 500 VMs, your JSON body should look like this:
{
"kind": "vm",
"length":500,
"offset":0
}Note: Offset is the index number. You need to tell the cluster/PC to get 500 VMs (length = 500) and to start at “index = 0” (offset = 0)
-
To get the next 500 VMs your JSON body should look like this:
{
"kind": "vm",
"length":500,
"offset":500
}Note: You need to tell the cluster/PC to get 500 VMs (length = 500) and to start at “index = 500” (offset = 500) → the “index” starts at 0 so the second request would need to be 500 (we are telling the API call to start at “index = 500” so we don’t duplicate information and we get the correct information)
-
To get the last batch of VMs, your JSON body should look like this:
{
"kind":"vm",
"length":500,
"offset":1000
}Note: You need to tell the cluster/PC to get 500 VMs (length = 500) and to start at “index = 1000”.
In this example that will get all 1407 VMs. The code example I included above included a python script that determines how many VMs are in the cluster/PC and makes the appropriate number of calls and adjusts the JSON body included in the v3 API call. I haven’t figured out how to do that in PowerShell yet so I just did it manually.
Hopefully this helps someone save a few minutes...