Resetting a lab environment

This post was last updated on February 21st, 2014 at 09:00 pm

I’m trying to get some automation working around my VCAP Test Track environment.

The latest addition is the ability for the user to reset the environment from the inside, which requires some hoops since they don’t have rights outside of the kit and are pretty much locked onto the RDP desktop.

My solution was to add a  PowerShell “wrapper” script running on the host to start the environment and then loop, watching for the creation of a file inside the kit.  Since all of the disks in the kit are non-persistent and making changes takes some doing, I also had the script create the reset command file on the desktop inside the kit so the user would find it

Step 1, kick off the environment (see vSphere Scripting and Automating your vSphere Demo)

start-process C:\DCA\ResetLab.cmd

Step 2: perform infinite loop. It’s not pretty but very effective!

do {

Step3: Look to see if the reset command file exists yet.  If not, try to map a drive letter (which establishes credentials to the VM) and create the reset command file.  Note all the .cmd file does is create the text file I’m looking for.

if (!(test-path "\\dc-troy-a.cert.vmeduc.com\c$\Users\Administrator\Desktop\reset and reboot lab.cmd")) {
 net use z: \\dc-troy-a.cert.vmeduc.com\c$ /user:administrator troVDCA5!
 add-content -path "\\dc-troy-a.cert.vmeduc.com\c$\Users\Administrator\Desktop\reset and reboot lab.cmd" "echo > \Users\Administrator\Desktop\restart.txt" 
 }

Step 4: Check to see if the restart file exists, if so delete the restart file and kick off the reset.  I have to delete the file or several copies of the reset will kick off before the path drops.

if (test-path \\dc-troy-a.cert.vmeduc.com\c$\Users\Administrator\Desktop\restart.txt) {
 del "\\dc-troy-a.cert.vmeduc.com\c$\Users\Administrator\Desktop\restart.txt"
 start-process C:\DCA\ResetLab.cmd
 }
 }

Loop, sweet loop.

 while ($true)

It’s not the prettiest, with no error checking the initial If fires off several times and fills the window with angry red text.  But it works, which is what counts.

I also set the hosts to auto-login and dropped the “wrapper” into Startup.  So now I can go from powered off to up and ready with one button.  <insert Viagra joke here>

Next step if scoring scripts inside the kit so participants can see if they achieved the correct solution.

 

This entry was posted in Certification, Cloud, Computing, PowerShell, Scripting, Virtualization, VMware. Bookmark the permalink.

Leave a Reply