One internal customer I have been working with for a while never stops to throw new challenges at me. Initialy the task was to set up 30+ VMs for the customer. Then we had to work through a situation where they wanted to take snapshots, but keep them for months at a time. Snapshots under my watch are not being kept that long. If a customer needs a snapshot for that long, we’d rather take a backup or clone the VM as needed.
Once that situation was under control another challenge showed up and we had the need to automate the process of setting VMDK disks to non-persistent and then back to persistent. This particular case was for an always changing number of VMs and so I scripted it out. Now all the customer has to do is to create a matching CSV file and the script will toggle the disks in whatever direction they need to go.
The last challenge was another clone operation. The customer had a new software installation done on one machine (a Red Hat Linux VM) and it turned out to be a major pain in the rear to do this same task on 5 other virtual machines. So, he wanted to have those VMs cloned.
Cloning a single VM is an easy task, but going through the GUI to do this for several machines is just not what I wanted to do – especially if the chances were high that this would happen again (and again). So, I decided to do this via PowerCLI and to let the customer do the dirty work by giving me a CSV file with all the information pre-populated the way I wanted it. That way I could push all the responsibility to the customer and all I had to do was to execute the script and then walk away.
Here is what the script does – step by step.
1. Import the CSV file
2. Power off the VMs in the “nameold” column
3. Permanently delete the VMs in the “nameold” column
4. Clone the source VM into as many VMs listed in the “namenew” column onto the same storage where step 3 had deleted the old VMs
By using PowerCLI I was able to script this out properly and only had to point to the CSV file upon execution. The script would wait for each task to complete before moving on to the next one. All I had to do was to email the customer once the clones were done so that he could customize the remaining pieces inside the VM. By using PowerCLI I had to do the work once, but for whenever the same request would come back I would just execute the script and have the customer worry about the CSV file.
Download the PowerCli Script here.