Tuesday, May 3, 2011

ESXi Automation (KMS Post Followup)

I finally got around to creating a small script which automates the process of starting up all of the machines outlined in the post, KMS Activation With Less than 25 Machines and VMWare ESXi after I found that the counter in the Windows KMS server resets itself after 30 days. This is of course OK with current machines, but when adding more machines it is not of course desirable.

Utilising a script called esxi-control.pl we are able to do this on a Windows machine with vCLI installed (preferably the KMS machine itself).

You will now need to decide on a method of reactivating the clients. I opted to make a startup script that ran "slmgr /ato", but you can also use pfexec (if you have set a password on the initial account). If you opt to create a startup script, you will need to create a new additional snapshot for each "machine" to save it (obviously you should include this in the image building process outlined in the previous entry).

Once you have decided on this, create the following batch script on the server you have installed vCLI on:

@echo off
REM Note: virtual machine must be powered on for script to change to a different snapshot, this script overcomes this with the two if statements below.

SET numofmach=24
SET seconds=60

EditV32 -p "Enter a password: " -m passwd

SET /a count=%numofmach%+1
FOR /L %%i IN (1,1,%count%) DO (
REM Start machine
esxi-control.pl --server esxi --username root --password %passwd% --action poweron --vmname kms-maintain

IF NOT "%%i"=="1" (
echo Waiting for machine to start

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %seconds% -w 1000 > nul

REM Activate, you could use psexec here:
REM psexec \\kms-maintain -u user -p blah "slmgr /ato"
REM or just use a startup script on machine
)

IF NOT "%%i"=="%count%" (
REM Change to next snapshot
esxi-control.pl --server esxi --username root --password %passwd% --action revert-to-snapshot --snapshotname activate-%%i --vmname kms-maintain
)

Now note that this script uses EditV32 to mask the text input when obtaining the password of the ESXi server. Obtain the appropriate version from it's webpage and put that in the same directory as the above batch script and esxi-control.pl. Once set let it run. If you find that the number of seconds is not enough, just increment it at the top. You can also modify this script to not get a password for the server if you like.

No comments:

Post a Comment