Archive

Tag Archives: vmware

This one might seem rudimentary for those of you who have a lot of PowerCLI fu, but it saved this n00b a few hours of time.  I found a condition where our automation software didn’t add a static route on a few Windows VMs.  The job now entails RDP’ing into each one and adding the route to our backup servers.  A not so great way of killing an afternoon if you have more than 5 VMs that needed this but necessary to do because having backups is a GoodThing(tm).

Enter PowerCLI and Invoke-VMScript.  You can supply Invoke-VMScript the ESX server username/password, VM username/password, (optionally the vCenter Server or login to it beforehand with Connect-VIServer), the VM name and the command you want to run.  You can add multiple commands by inserting a double ampersand between each command.  On Linux, && works out of the box because the ScriptType is bash by default.  For Windows, you have to specify ScriptType to be bat for batch files because it uses PowerShell by default and && will throw an error.  For example:

Invoke-VMscript -HostUser root -HostPassword qwerty12345 -GuestUser Administrator -GuestPassword yuiop67890 -ScriptType “bat” -ScriptText “route -p add 10.0.0.0 mask 255.255.255.0 10.10.0.1 && route print && ping backup01” -VM dbserver01

In this example, I’m adding a persistent static route, printing the route table to verify it was added and then pinging the backup server to make sure all is well.  One nice side note is that I don’t have to look up the management interface of each VM I want to run this on.  I also don’t have to look up which ESX server my VM is running on.  I’ve listed the VM name as the last parameter so I can easily up arrow and replace the VM name.  At some point, I’ll figure out how to loop through to make this even more hands off.

Doing this has freed me up to go back to other tasks at hand like using backup software documentation to cure insomnia!