Quickly power on virtual machines with PowerCLI Get-View

I recently had a need to power on a lot of virtual machines in a specific order. I was given several text files each numbered with a startup sequence. At a specific time I would be given a number and need to power on all of the virtual machines associated with that startup sequence number. Several minutes later I would be given another number and need to power on all of those virtual machines. Using Get-VM and Start-VM really wasn’t an option… even with -RunAsync that option was kind of slow.

The option I finally settled on involved using Get-View to return a list of all virtual machines; I would then use Where-Object to find the specific VM and finally invoke the PowerOnVM_Task to start the VM. During some testing, I realized this was almost too fast. Nearly all of the power on tasks would queue up in vCenter and all the VMs would power on in very large batches. To keep things somewhat controlled, I added a very short .5 second delay between virtual machine power on tasks.

1
2
3
4
5
6
7
8
9
10
11
12
$serverList = Read-Host "Which list would you like to power on? "
$view = Get-View -ViewType 'virtualmachine' -Property Name

$taskTracker = @()
Get-Content "$serverListList.txt" | %{
  $vmName = $_
  Write-Host "Now powering on VM: $vmName ... " -nonewline
  $taskTracker += ($view | ?{$_.name -eq "$vmName" }).PowerOnVM_Task($null)
  Start-Sleep -Milliseconds 500
  Write-Host "done!"
}
"Submitted $($taskTracker.Count) power on requests"

2013/10/20 Update: Just to clarify, I had files named 1list.txt, 2list.txt, 3list.txt and so on, each containing a virtual machine name listed one per line of the file. When the Read-Host cmdlet asked ‘Which list would you like to power on?’ I just answered 1, 2 or 3. The script automatically appends the list.txt to the file name on the Get-Content line.

This script can potentially throw some errors that are not accounted for in code, but weren’t a show stopper in my situation. First of all, this script does not account for vApps or scenarios where multiple virtual machines have identical names. vApps typically have their own startup order defined within the app. This process will turn on member VMs but may or may not be in the correct order. Another problem would be if you have two virtual machines with the same name, such as “UI VM”. This script will not turn on either (however it could be modified slightly to do so). The last issue I’m aware of is that this process will attempt to turn on any virtual machine in the list without first checking to see if the virtual machine is currently running. If a VM was powered on before my script reached it, the error “The operation is not allowed in the current state” was logged in vCenter and in the script. In my situation this was acceptable.

2 comments

  1. bixit says:

    Hi,

    Could you let us know how the input file is formatted?

  2. Sorry for the very long delay… but to answer this question, the input file would be formatted with one VM name per line. If you have a CSV input file you’d rather use, you could change line 5 to use Import-Csv (instead of Get-Content) and then update line 6 from $vmName = $_ to $vmName = $_.ColumnNameContainingVM.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.