VMware VI Toolkit for Windows – Tip: Regular Expressions

The importance of having a naming schema for your virtual machines in VMware VI (ESX / Virtual Center) has become much more important with the release of the VI Toolkit for Windows.

If you’re not sure what the VI Toolkit for Windows is, please see a post that I wrote earlier this month: VMware VI Toolkit For Windows.

The VI Toolkit can allow you to automate and script a lot of tasks that would require quite a bit of UI interaction with the VI client.  Something that you can do to facilitate this effort is by naming your virtual machines with a consistent pattern.  A typical naming convention might end with a tag about the server’s function and a number representing the server’s redundancy.

An example of this is might be web servers ending with -wb01, -wb02, and so on.  Exchange servers ending in -ex01, -ex02, -ex03, and so on.

With a naming convention similar to that, you can begin using regular expressions to select batches of virtual machines to perform functions against.

Following the pattern above, you could select all your web servers with the following statement:

     get-vm | where {$_.Name -match "-wb[0-9][0-9]"}

and your exchange servers with:

     get-vm | where {$_.Name -match "-ex[0-9][0-9]"}

By piping these virtual machines to other commands, you can execute tasks across blocks and groups of virtual machines.  If you wanted to upgrade the VMware Tools on every web server in your environment, you could use the command:

     get-vm | where {$_.Name -match "-wb[0-9][0-9]"} | update-tools

Careful usage of the regular expression match clause in the command could also allow you to upgrade only specific web servers.

     get-vm | where {$_.Name -match "-wb0[1,3,5,7,9]"} | update-tools

This will select the servers whose names end with -wb01, -wb03, -wb05, -wb07, and -wb09.

With the power of regular expressions and some thought into your virtual machine naming convention, there is a lot that you can do with a single line of script.

Happy scripting!

Helpful link: PowerShell – Writing Regular Expressions


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s