PowerCLI Wildcards

It seems like every day I'm reminded just how helpful PowerShell (and specifically PowerCLI) is during normal server administration.  Sure, you can do those tasks in the GUI with a few mouse clicks (and a moderate amount of waiting for the web interface to draw windows...), but PowerCLI can get it done so much faster!

For example, one of my customers had a collection of ESXi hosts that needed to be moved into a cluster.  Rather than dragging each one into it, I looked up the PowerCLI command (get-vmhost ESXiServer | move-vmhost -destination ClusterName).  Once you can do it for one host, it takes even fewer characters to do it to all hosts (get-vmhost | move-vmhost -destination ClusterName).  The part that made me smile was that it was also trivial to select the specific range of hosts that I wanted to move by simply using: get-vmhost *esx0[1-8]* | move-vmhost -destination ClusterName.

What that command did, for anyone who isn't familiar with that syntax, is to move into the cluster ESXi hosts 01 through 08.  It does this by checking their names to see if they contain the string "esx0" and then any number between 1 and 8.  It then passes all of those returned hosts to the "move-vmhost" cmdlet and it all takes care of itself.

I used this technique for a pretty simple task (admittedly, I could have used the right hand panel of the interface to shift-select all 8 hosts and then just drag them to the cluster in the left panel to move all 8 at once in the web interface), but the joy of PowerCLI is that this exact same technique can be used for any host configuration command.  Need to configure NTP across a large set of hosts, but don't have a Host Profile?  This trick'll work.  How about building a bunch of standard vSwitches?  This'll do it.  How about configuring a bunch of ESXi hosts' scratch locations?  Try these commands on for size (after mounting the volume as described in that KB article and replacing the <LUN ID> bit with the actual desired LUN ID for your log LUN):

for ($i=1; $i -le 8; $i++){new-item "esx0$i\scratch" -itemtype directory}

foreach ($thisHost in get-vmhost esx0[1-8]*){$thisHost | Set-VMHostAdvancedConfiguration -name "ScratchConfig.ConfiguredScratchLocation" -Value "/vmfs/volumes/<LUN ID>/$(($thisHost.name).substring(0,($thishost.name).indexof(".")))/scratch"}

It's just one more technique to add to your toolbelt, but it combines so nicely with so many others that I becomes quite powerful and is certainly worth being aware of.

Update: Check out this other post about setting up ESXi scratch locations for a much more refined set of commands (and the whole process), if that's what's caught your interest.


Popular posts from this blog

Deleting Orphaned (AKA Zombie) VMDK Files

Clone a Standard vSwitch from one ESXi Host to Another

vCenter Server Appliance Crash due to Full /Storage/SEAT Partition