VMware Cloud Community
mat_ban
Contributor
Contributor
Jump to solution

How to start policy via script? (How to query policies via script?)

Hi guys

I would like to be able to start a vRO Policy by a script. I have already found com.vmware.library.policy.startPolicy()in the API browser and would like to pass one of my vRealize Orchestrator Policies to this method. Unfortunately I do not find a way to query the current policies so far. My ugly workaround therefore currently consists of starting a helper-workflow with a manually pre-assigned policy.


How can I do this in pure Javascript?


Best regards


mat.ban

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi mat.ban,

Finding policies (and other vRO objects) is quite easy - there are generic build-in scripting methods Server.findAllForType(type) and Server.findForType(type, id) that find all objects of a given type, and an object of a given type having a given ID, respectively.

Here are a couple of code snippets demonstrating how to fetch all policies (the first snippet) and a concrete policy by its ID (the second snippet):

var allPolicies = Server.findAllForType("Policy");

if (allPolicies == null || allPolicies.length == 0) {

  System.log("No policies found.");

} else {

  for each (var plc in allPolicies) {

    System.log("policy id: " + plc.id + " name: " + plc.name + " description: " + plc.description);

  }

}

var plc = Server.findForType("Policy", "8a97a01f55c5f7ee01562c3a27d5011d"); // the second parameter is your policy ID

if (plc == null) {

  System.log("No policy found.");

} else {

  System.log("policy id: " + plc.id + " name: " + plc.name + " description: " + plc.description);

}

View solution in original post

0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi mat.ban,

Finding policies (and other vRO objects) is quite easy - there are generic build-in scripting methods Server.findAllForType(type) and Server.findForType(type, id) that find all objects of a given type, and an object of a given type having a given ID, respectively.

Here are a couple of code snippets demonstrating how to fetch all policies (the first snippet) and a concrete policy by its ID (the second snippet):

var allPolicies = Server.findAllForType("Policy");

if (allPolicies == null || allPolicies.length == 0) {

  System.log("No policies found.");

} else {

  for each (var plc in allPolicies) {

    System.log("policy id: " + plc.id + " name: " + plc.name + " description: " + plc.description);

  }

}

var plc = Server.findForType("Policy", "8a97a01f55c5f7ee01562c3a27d5011d"); // the second parameter is your policy ID

if (plc == null) {

  System.log("No policy found.");

} else {

  System.log("policy id: " + plc.id + " name: " + plc.name + " description: " + plc.description);

}

0 Kudos
mat_ban
Contributor
Contributor
Jump to solution

Hi Ilian Iliev,

thank you very, very much.

I didn't find the right documentation, because I have been searching for "policy" and "policies" in the API Explorer. That's why I missed Server.findAllForType(type) and Server.findForType(type, id).

Have a nice day!

mat.ban

0 Kudos