VMware Cloud Community
adamflaig1
Enthusiast
Enthusiast

Get Virtual Machines by Name - Workflow (How to RegEx)

I thought I would put a post out there to help my fellow Orchestrator developers.

Problem: Searching for Virtual Machines Case Insensitive

Orchestrator comes with a workflow that you can use to search for Virtual Machines that accepts Regular Expressions (RegEx) to help.

Library/vCenter/Virtual Machine Management/Basic/Get virtual machines by name

GVMBName.PNG

Breakdown (What does it do out of the box?)

Short Story:  This workflow takes a single sting input and outputs an array of matching VC:VirtualMachine Objects.

Long Story:  This workflow takes a single sting input Foreach vCenter sdk connections through the VcPlugin it calls another workflow.

Library/vCenter/Virtual Machine Management/Property collector/Get virtual machine by name with PC

GVMBName1.PNG

This workflow collects additional information and calls a third workflow in the same folder

Library/vCenter/Virtual Machine Management/Property collector/Get vCenter entity by properties

GVMBName2.PNG

This workflow collects some vCenter Objects based upon the properties.  In this case we the workflow is searching for Virtual Machine objects with a matching property name.

If you are newer to Orchestrator or may be confused as to why things are broken down this far you may be wondering, Why did they break things down this far?!?  Why not just put all this in one workflow and give is a simple way to do what I want?

The answer is as brilliant as the workflow itself. Breaking this down into smaller chunks makes the process simple to understand and the individual pieces are very powerful.  Once you start playing with the child workflows you'll understand the power behind them.  I won't break apart these pieces here that’s not the intent of the article.

Passing RegEx

I can't give a tutorial on regular expressions here. There are several articles and cheat sheets online to help you figure out 'How to RegEx'.  My example below will show how to pass a RegEx value to the input of the workflow to search the environment for a Virtual Machine case insensitive.

Those that are familiar with RegEx will simply try a global flag to make the search parameter case insensitive like this which does not work.

DOES NOT WORK

/Windows16Server/i 

First Rule:  You can only pass what is inside the slashes.  Sorry can't set global flags.

Second Rule:  You can't use, at lease I haven't found a way, to set the inline flag (?i) preceding your text so the following also does not work.

DOES NOT WORK

(?i)Windows16Server

Now that we have covered what doesn't work What does work?

Punch Line

You need to be able to search upper and lower case letters to match the server name you are looking for.  Here is an example of what works.  This is assuming that you have a server that matches the name.

WORKS (Windows16Server broken out into upper and lowercase letters)

^(w|W)(i|I)(n|N)(d|D)(o|O)(w|W)(s|S)16(s|S)(e|E)(r|R)(v|V)(e|E)(r|R)$

Now before you throw your hands in the air and run away we can build this on the fly and wrap this workflow in another workflow.

Solution

A quick note about how I develop.  It is easy to get lost in Orchestrator forget where you defined your variables which is important.  I follow a simple coding practice to keep track of things.  All variables are prepended with a I_, A_, O_, or L_

I_ are defined workflow Inputs

A_ are defined workflow Attributes

O_ are defined workflow Outputs

L_ are local variables inside of Scriptable objects and these values won't be passed to anywhere else in the workflow.

Create a workflow I called mine findVMs

GVMBName3.PNG

Inputs TAB

Input Variable

I_ serverName TYPE: string Description: Case insensitive Search for VMs

General TAB

Attributes

A_searchName TYPE: string Description:  RegEx Value based upon String Input

A_vms TYPE: Array/VC:VrtualMachines Description: Array of Virtual Machines found

Outputs

Output Variables

O_targetVM TYPE: VC:VirtualMachine Description: Target VM Object

Schema TAB

Create a Scriptible object called Create RegEx IN is going to be the Workflow Input variable I_serverName OUT is the workflow Attribute A_searchName

CODE Create RegEx

var L_tmpName = I_serverName.toLowerCase();

var L_searchName = '^';

var L_splitName = L_tmpName.split('');

var L_entity = new Array();

for (i in L_splitName) {

  if (L_splitName[i].match(/[0-9]/)) {

    L_entity = L_splitName[i];

  } else {

    L_entity = '(' + L_splitName[i] + '|' + L_splitName[i].toUpperCase() + ')';

  }

  L_searchName += L_entity;

}

L_searchName += '$';

A_searchName = L_searchName;

System.debug(L_searchName);

Next Add our favorite Workflow Get virual machines by Name and attach the input to our workflow attribute A_searchName and our Output to our workflow attribute A_vms

Create a third object to our workflow to parse the array of objects to only select a single VM and pass that VM to the output parameter of the workflow.  Call this scriptable Object GetVM from Array IN our workflow attribute A_vms and OUT our workflow attribute O_targetVM

CODE Get VM from Array

for (i in A_vms) {

  System.log(A_vms[i].name);

  O_targetVM = A_vms[i];

}

Results

What outputs is a single VM object found by a case insensitive search that you can take to other workflows and perform work.

Adam Flaig
0 Kudos
0 Replies