After I got NUT sorted out on my *nix fleet it was time to bring my Windows box into the mix, I frequently run VM Guests on my workstation for various different tasks, basically unless it’s something that’s going to be providing an active service for an extended period I run it up in VMWare Player, any “servers” go on the ESXi box.

Because VMWare doesn’t gracefully shut down or suspend VM Guests when the host gets shut down there was the possibility of badness as a result of an abrupt windows shutdown in the event of a power failure, as such I wanted any Guests which happened to be running on my workstation to be gracefully shutdown (or rather suspended).

Initially I’d planned to do this with the VMWare VIX API, but in doing some digging into it and trying a few things I decided it was too much bother for the moment (it’s also, frankly, a pretty damn ugly interface), so we went to Plan B which was to screenscrape the output of vmrun to determine what VMs were active, then issue a suspend to each of them prior to shutting down the host.

This is what I ended up with (It’s called from a batch file which is called by upsmon), I’m kinda new to this PowerShell stuff, so this is pretty horrific, but it does the job.

pushd
cd "C:\Program Files (x86)\VMware\VMware VIX"
$vms = .\vmrun.exe -T player list
foreach($v in $vms)
{
	if ($v -like "*.vmx") {
		$stat = "Suspending " + $v;
		echo $stat;
		$vm = '"' + $v + '"';
		echo $vm;
		.\vmrun -T player suspend $vm
	}
}
popd

The only other minor hitch was that in order to be able to see the active VMs I needed to switch the Network UPS Tools service over to using my account rather than LocalSystem, but I figure that’s probably better anyway, least (or rather lesser) privilege and all of that.