如何通过Postman请求获取vSphere指定文件夹下的虚拟机以实现按文件夹恢复?
Great question! Let's walk through how you can achieve this with the vSphere REST API—there isn't a single out-of-the-box endpoint that returns the nested folder-VM structure you're looking for, but we can build it using existing endpoints and a bit of client-side processing.
核心思路
vSphere REST API separates folder and VM resources, so we'll use folder IDs to filter VMs and then combine the results into your desired structure. Here's the step-by-step approach:
1. 获取目标文件夹的ID
First, use the folder endpoint with a filter to get the unique ID of your target folder (this avoids fetching all folders when you only care about one).
Run this GET request:
GET https://SERVER/rest/vcenter/folder?filter.names=my_folder
The response will include the folder's ID (look for the folder field in the value array), e.g.:
{ "value": [ { "folder": "folder-123", "name": "my_folder", "type": "VM", "parent_folder": "folder-45" } ] }
2. 过滤获取该文件夹下的所有虚拟机
Next, use the folder ID from step 1 to filter the VM endpoint and get all VMs in that folder.
Run this GET request (replace folder-123 with your actual folder ID):
GET https://SERVER/rest/vcenter/vm?filter.folders=folder-123
This returns all VMs directly under the specified folder.
3. (可选)合并结果为你想要的结构
If you want the combined folder + VMs structure you described, you can use Postman's test scripts to automate merging the two responses. Here's a sample JavaScript script you can add to the Tests tab of your folder request:
// Grab the folder data from the current response const folderData = pm.response.json().value[0]; // Send a request to fetch VMs in this folder pm.sendRequest({ url: `https://SERVER/rest/vcenter/vm?filter.folders=${folderData.folder}`, method: 'GET', headers: pm.request.headers.toJSON() // Reuse your existing auth headers }, (err, vmResponse) => { if (!err) { const vmList = vmResponse.json().value; // Build the desired output structure const combinedResult = { "value": [ { "folder": folderData.name, "VMs": vmList } ] }; // Log the result to Postman's console console.log("Combined Folder + VMs:", combinedResult); // Optional: Store the result in an environment variable for later use pm.environment.set('folderWithVMs', JSON.stringify(combinedResult)); } });
4. (进阶)包含子文件夹中的虚拟机
If you need VMs in nested subfolders too, you'll first need to recursively fetch all child folder IDs under your target folder, then include all those IDs in the VM filter. For example:
GET https://SERVER/rest/vcenter/vm?filter.folders=folder-123,folder-456,folder-789
You can extend the Postman script to handle this recursion by looping through subfolders until you've collected all relevant IDs.
关键说明
The vSphere REST API is designed to keep resources decoupled (folders and VMs are separate entities), which is why there's no single endpoint that returns the nested structure directly. But with the filtering capabilities and a small amount of client-side logic, you can easily build the exact output you need.
内容的提问来源于stack exchange,提问作者Drak432

