VM Start-up script

Back in Jan 2010 I wrote a script which shut down the virtual infrastructure, this was used to ensure your VMs were shut down and then your hosts were shut down in an orderly manner, ideal for things like UPS shutdown scripts etc.

A slight spin on that is a Start-Up script, I was asked by a couple of people if I had one of these, it also becomes more relevant with the vCenter Virtual appliance, no need for HA and DRS rules or to manually search for the vCenter Virtual Appliance if there is a power outage.

The script below will connect to all hosts you define at the start, these could obviously be read from a csv, xml file or a database etc but I just define them in a variable for ease of the script.

The script will firstly connect to each of these hosts in turn, once connected it will search for the first VMs, in my case this is a couple of domain controllers, it could be database servers or any other infrastructure servers which need to come up before vCenter.   Once these VMs have been found the script will start them and wait for their tools status to be ready, this is an easy way to check that the VM is started completely.

Once the infrastructure servers are started it will then search for your vCenter VM, this could be on any of the connected hosts, it will start it and again wait until the tools return as being completely started.

The Script

$MyHosts = "192.168.0.101", "192.168.0.102"
$username = "root"
$password = "p@$$w0rd"
$DomainSRV = "DOMAIN01", "DOMAIN02"
$VirtualVC = "vCenter01"

$AllowMultiple = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false

$MyHosts | Foreach {
Write-Host "Connecting to host: $($_)"
$connection = Connect-VIServer $_ -User $username -Password $password
}

$VM = Get-VM $DomainSRV
$VM | Foreach {
Write-Host "Starting $($_.name) found on $($_.VMHost)"
$StaringVMs = Start-VM $_ -Confirm:$false -RunAsync
}

$VM | Foreach {
do {
$VM = Get-VM $_
$Toolsstatus = $VM.ExtensionData.Guest.ToolsRunningStatus
Write-Host "Waiting for $VM to start, tools status is $Toolsstatus"
Sleep 7
} until ($Toolsstatus -eq "guestToolsRunning")
}

$StartVC = Get-VM $VirtualVC | Start-VM -Confirm:$false

do {
$VM = Get-VM $VirtualVC
$Toolsstatus = $VM.ExtensionData.Guest.ToolsRunningStatus
Write-Host "Waiting for $VM to start, tools status is $Toolsstatus"
Sleep 7
} until ($Toolsstatus -eq "guestToolsRunning")

Write-Host -ForegroundColor Yellow "vCenter Server started, please connect as normal"


In Action

3 thoughts on “VM Start-up script

  1. Mariusz

    Hi,
    I’ve tried to test vCenter Appliance using “$Toolsstatus = $VM.ExtensionData.Guest.ToolsRunningStatus”, but it reports “guestToolsRunning” much earlier then it start-up completely.

    Do you have some idea, how to test vCenter Appliance?
    Best regards,
    Mariusz

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.