Sequencing virtual machines startup script

January 17, 2014 – 7:56 pm

Whether it’s for disaster recovery, maintenance or for your home lab, it’s nice to be able to start-up machines in a specific order without having to do it manually. You can do it with vApps, but there are operational drawbacks in doing so, from those pesky warnings when you shutdown/startup an individual VM, to the simple fact that a vApp is also a resource pool, which might not be something you need/want.

So I wrote this basic script to solve this problem. It’s really useful for my home lab, since I shut down everything when I’m done working. This way, I can simply fire up my hosts, run the sequencing script and there you go, back in action.

To ensure that VMs are started when the required services are available, configure your VM Tools service dependency to whatever service MUST be running before the next VM starts.

So here’s the script:

##############################################################################################################
#
# .SCRIPT INFO
#   Name       : SeqStart.ps1
#   Author     : Marc Bouchard
#   Revision   : January 16th, 2014
#
# .DESCRIPTION
#   This script starts VMs in the priority order defined in configuration files.
#   User is prompted for credentials for vCenter/Hosts
#
# .PARAMETERS
#   No parameters required
#
# .INPUT FILES
# 000-Hosts.000         = Contains the list of hosts to poll - can be individual hosts or vCenter server(s)
# 00x-description.00x   = VMs to load in specific order, one per line
#
# .EXAMPLE 1
#   PS> SeqStart
#
# .TO DO LIST
#
##############################################################################################################

# Parameters
Param (
    [Parameter(Mandatory=$false )] [string] $StrCounter = "001"
)

# Initialize
Clear-Host
Set-PowerCLIConfiguration -invalidCertificateAction "ignore" -confirm:$false | Out-Null

##############################################################################################################


# Ask for root credentials for ESXi hosts

while ($vCenterCreds -eq $null) 
{
   Write-Host (get-date -uformat %I:%M:%S) "Getting authentication credentials for hosts" -ForegroundColor Green;
   $vCenterCreds = $host.ui.PromptForCredential("VMware ESXi Login", "Please provide ESXi Hosts credentials", "", "")
}

# Check if file exists
if (Test-Path "*.000")
{
   # Read Host names from text file
   Get-Content "*.000" | Sort | ForEach-Object {Connect-VIServer $_ -Credential $vCenterCreds}|Out-Null
}
else
{
   # Error message
   Write-Host (get-date -uformat %I:%M:%S) "The specified Hosts file does not exist." -ForegroundColor Red;
   Exit
}

# Loop through the numbered tasks lists

$NumCounter = 1
$StrCounter="{0:000}" -f $NumCounter

while (Test-Path "*.$StrCounter" ) 
{
   # Read the file contents
   $VMList = Get-Content "*.$StrCounter"
   
   foreach ($VM in ($VMList))
      {
         # If the VM exists, start it!
         $ItExists = Get-VM -Name $VM
         if ($ItExists) {
            # Start VM and wait for VM Tools to be ready (VM Tools have to be setup for service dependency if required inside the VM)
            Write-Host (get-date -uformat %I:%M:%S) "Starting up $VM" -ForegroundColor Green
            Start-VM -VM $VM | Wait-Tools | Out-Null
         }
         else
         {
            Write-Host (get-date -uformat %I:%M:%S) "$VM does not exist" -ForegroundColor Red
         }
      }
   
   $NumCounter++
   $StrCounter="{0:000}" -f $NumCounter
}
# End of loop

# End of script

Post a Comment