VMware Cloud Community
hadjer1
Enthusiast
Enthusiast
Jump to solution

get all VMs in Aria automation project

Hello,

I need an aria orchestrator script that takes as entery a project name and returns all the VMs in this project, can anybody help with this ?

0 Kudos
1 Solution

Accepted Solutions
xian_
Expert
Expert
Jump to solution

var project = Server.findAllForType("VRA:Project").filter(
    function (project) {
        return project.name == projectName;
    }
)[0];

if (!project) throw "Project '" + projectName + "' not found.";

System.log(project.id);

var machines = Server.findAllForType("VRA:Machine").filter(
    function (machine) {
        return machine.projectId == project.id;
    }
);

for each (machine in machines)
    System.log(machine.name);

xian__0-1707467348758.png

The user must have admin permissions to list projects and machines.

 

View solution in original post

3 Replies
DanielStastka
Enthusiast
Enthusiast
Jump to solution

You must create 2 Rest-API Call

1) to get Id from Project if don't now the id

2) Get all Machine with Filter ProjectId or Name

Return value is Array of Properties

 

var vraHost = VraHostManager.findHostsByType("vra-onprem").filter(
    function (host) {
        return host.name == 'Default'
    }
)[0];

var filter = "?$filter=id+eq+%27" + project + "%27+or+name+eq+%27" + project + "%27";
var restClient = vraHost.createRestClient();
var request = restClient.createRequest("GET", "/iaas/projects" + filter, "{}");
var response = restClient.execute(request);

var vAaProjects = JSON.parse(response.contentAsString).content
var returnValue="";
for each (vAaProject in vAaProjects){
   
    System.log("name:"+vAaProject.name);
    System.log("description:"+vAaProject.description);
    System.log("id:"+vAaProject.id);
    System.log("costcenter:"+vAaProject.customProperties.project_costcenter);
}
var filter2 = "?$filter=projectId+eq+%27" + vAaProject.id + "%27";
var restClient2 = vraHost.createRestClient();
var request2 = restClient2.createRequest("GET", "/iaas/api/machines" + filter2, "{}");
var response2 = restClient2.execute(request2);

var vAAdeploymentsFromProject = JSON.parse(response2.contentAsString).content

var vmArray = [];
for each (vAAdeploymentFromProject in vAAdeploymentsFromProject){
  
   
    System.log("---------------------------");
    System.log("hostname:"+vAAdeploymentFromProject.hostname);
    System.log("deploymentId:"+vAAdeploymentFromProject.deploymentId);
    System.log("softwareName:"+vAAdeploymentFromProject.customProperties.softwareName);
    System.log("osType:"+vAAdeploymentFromProject.customProperties.osType);
   
   var prop = new Properties();
    prop.put("hostname",vAAdeploymentFromProject.hostname);
    prop.put("deploymentId",vAAdeploymentFromProject.deploymentId);
    prop.put("softwareName",vAAdeploymentFromProject.customProperties.softwareName);
    prop.put("osType",vAAdeploymentFromProject.customProperties.osType);
    vmArray.push(prop);
}


return vmArray;

 

 

xian_
Expert
Expert
Jump to solution

var project = Server.findAllForType("VRA:Project").filter(
    function (project) {
        return project.name == projectName;
    }
)[0];

if (!project) throw "Project '" + projectName + "' not found.";

System.log(project.id);

var machines = Server.findAllForType("VRA:Machine").filter(
    function (machine) {
        return machine.projectId == project.id;
    }
);

for each (machine in machines)
    System.log(machine.name);

xian__0-1707467348758.png

The user must have admin permissions to list projects and machines.

 

hadjer1
Enthusiast
Enthusiast
Jump to solution

Hello,

Thank you @xian_ and @DanielStastka for your answers.

@xian_that was exactly what i was looking for !

Regards.

0 Kudos