Quantcast
Channel: dbailey's blog » VMWare
Viewing all articles
Browse latest Browse all 4

When you absolutely, positively got to kill [almost] every [VM] in the [infrastructure]…

$
0
0

We’ve had two instances that required us to shutdown our ESX infrastructure recently. The first was a data center move and the second was SAN maintenance. I wrote the following scripts to shutdown all the VMs we needed to and start them back up once the maintenance was completed.

Mass-VM-ShutDown.ps1

  1. This will first dump a list of all VMs powered on in the vCenter instance you’re connected to.
  2. Then, it will check your exception list if one was provided to make sure all of the exceptions actually exist. This way, if there’s a typo in the exception list that VM won’t be shut down accidentally.
  3. Then it will go through all of the powered on VMs, check to make sure they’re not on the exception list and try to do a guest OS shutdown. If the guest OS shutdown fails it will power off the VM.

The script logs everything to the specified log file and has a couple of prompts to make sure you’re doing what you want. Additionally, I added -whatIf parameters to the shutdown commands and a $forReal boolean for testing. To run this for real, you would need to set $forReal to true in the script and remove the -whatIfs. For testing, you’d want to put a few of your test VMs in else block of if ($forReal).

Start-VMs.ps1

This takes a text file (perhaps based on the Mass-VM-Shutdown script’s PoweredOnVMList… hint, hint) and starts VMs from that file in order. The file can be plain text or you can provide a .csv file where the VMs are ordered in batches you’ve prioritized so that you don’t slam your hardware by bringing things up all at once and you handle dependencies between VMs. If you provide a .csv, there should be column headings named “VM Name” and “Batch”. You give the script the path to your .csv and the batch number you want to start.

If you’re running PowerCLI 4.1 or greater, for each VM it runs a Get-VM to make sure it exists and that it’s powered off. This way the script can handle common errors more cleanly. It starts the VMs asynchronously and stores all of the task objects in an array. Errors that occur when the task is created are stored in custom task objects created using New-Object with the -Properties parameter so they’re easier to parse. If an error occurs as the task progresses, it’s a normal VMWare task object.

Once it kicks off all of the asynchronous Start-VM commands, it monitors the tasks until they’re complete and provides a nice progress bar. Then it outputs the results and saves the detailed results in a global variable that you can specify as a parameter.

If the PowerCLI is less than 4.1, it just tries to start each VM and writes an error.

One approach might be to create a .CSV file of all the VMs you know you’ll need to start ordered and batched as you see fit. Then, run the script again against the PoweredOnVMList generated by the mass shutdown script so that you pick up any stragglers that weren’t in your plan. You could also diff what you shutdown and what was in your .csv to reconcile things.

Click the headings to download the scripts with .txt extensions.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images