VMware Cloud Community
Naineyess
Enthusiast
Enthusiast
Jump to solution

Attribute type conversion for VMs .. ??

As I am capturing the entity name of the VMs in the form of string via vCO SNMP traps and I want to map this entity name with the VMs as VC:VirtualMachine which I am not able to do it now.. Please help me out with this..

Thanks,
Naineyess

1 Solution

Accepted Solutions
marcseitz
Enthusiast
Enthusiast
Jump to solution

Hi Naineyess,

we've done this by using a Action which expects the VM-Name and is searching and returning the VM-Object:

Create an action with the following Input / Output:

Input:

- Name: vmName

- Type: string

Output:

- Type: VC:VirtualMachine

Sourcecode:

if ( vmName == "" ) throw "Missing Parameter: Virtualmachine-Name!";

var myVirtualMachines = VcPlugin.getAllVirtualMachines();
for each (var myVirtualMachine in myVirtualMachines)
{
if ( vmName == myVirtualMachine.name )
  return myVirtualMachine;
}
return null;

Regards,

Marc

View solution in original post

5 Replies
marcseitz
Enthusiast
Enthusiast
Jump to solution

Hi Naineyess,

we've done this by using a Action which expects the VM-Name and is searching and returning the VM-Object:

Create an action with the following Input / Output:

Input:

- Name: vmName

- Type: string

Output:

- Type: VC:VirtualMachine

Sourcecode:

if ( vmName == "" ) throw "Missing Parameter: Virtualmachine-Name!";

var myVirtualMachines = VcPlugin.getAllVirtualMachines();
for each (var myVirtualMachine in myVirtualMachines)
{
if ( vmName == myVirtualMachine.name )
  return myVirtualMachine;
}
return null;

Regards,

Marc

Naineyess
Enthusiast
Enthusiast
Jump to solution

Hi Marc,

Thanks your response well I am trying with the same solution which you have provided.. but it is showing some error "Invalid return" ( for return myVirtualMachine;) ..

Thanks !

0 Kudos
marcseitz
Enthusiast
Enthusiast
Jump to solution

Did you set the Output of the Action? Or are you using the code directly as Scriptable Task in your Workflow?

0 Kudos
Naineyess
Enthusiast
Enthusiast
Jump to solution

Thanks a lot you saved my life ... Smiley Happy Smiley Happy

0 Kudos
Burke-
VMware Employee
VMware Employee
Jump to solution

The following method will provide much better performance when there are a lot of VMs in your inventory:

var sdkConnections = VcPlugin.allSdkConnections;

vms = new Array();

for (var i in sdkConnections) {

     var host = sdkConnections[i];

     var found;

     if (host.isInventoryServiceAvailable()) {

          found = host.getAllVirtualMachines(null, "matches(name, '" + criteria + "')");

     } else {

          found = host.getAllVirtualMachines(null, "xpath:matches(name, '" + criteria + "')");

     }

     for (var j in found) {

          vms.push(found[j]);

     }

}

The above script will search all vCenter connections. For each one, it will attempt to find a vm that has the matching name. It then adds the matching vms to an array called vms... Ideally, you would hope for only 1 match Smiley Wink . The technique shown here has the flexibility for you to add your own error checks after the matching vm(s) has been found.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter