Posts

Showing posts with the label Regular Expression

PowerCLI to Select Arbitrary Host Ranges via Regex

This is another quickie (I've had a very PowerCLI rich couple of months!).  We've been doing a lot of mass ESXi host manipulations lately and typically grab the hosts in batches of 8.  As such, I've needed to be able to target a specific set of 8 ESXi servers with some of my PowerCLI commands.  The first 8 (named esx01-esx08) are really easy to grab with string matching: get-vmhost *esx0[1-8]* That command will return all ESXi hosts in that range.  That same trick doesn't work so well with other batches of ESXi hosts (until you get into the esx41-48 range), as the square brackets represent only a single character.  That means that get-vmhost *esx[09-16]* is not a valid construct.  Fortunately, this is a pretty easy match to make with a regular expression : esx(09|1[0-6]) That regex works because of a few important symbols.  The | is the key to it; it means "or".  A simple example would be "09|10" which matches anything with "09" or ...