PowerCLI: Set VM Startup Policy


image

Update: I added a little script block which enables the Startup Policy on the ESX Host.

If you want to change the VM Startup Order under Configuration – Virtual Machine Startup/Shutdown by hand will costs you a lot of time. So I wanted to see if I was able to script these settings with PowerCLI.

You can check the Startup Order via:  Get-VMHost |Get-VMStartPolicy |Sort StartOrder

And this is how you can see if the Startup Policy is enabled: Get-VMHostStartPolicy              -VMHost ( Get-VMHost )

image

First, I created a CSV file:

VMName,StartupOrder

DC01,1

MC01,2

VC01,3

MAIL01,4

XPMC01,5


In this CSV file, I have entered my VM’s and the startup order value. I have tested it with multiple servers and it’s possible to enter two VM’s with the same StartupOrder value if the VM’s are placed on different ESX Hosts.

The following script imports the CSV file and loops through all the items and configures the VM Startup Policy:

$vCenter = Read-Host "Enter the vCenter server name"

Connect-VIServer $vCenter

$hosts = Get-VMHostStartPolicy -VMHost (Get-VMHost)
$vms = "c:\scripts\ps\vmsStartup.csv"
$csv = Import-CSV $vms

foreach($item in $csv){
    $vm = $item.VMName
    $order = $item.StartupOrder

    $vmStartup = Get-VMStartPolicy -VM $vm
    Set-VMStartPolicy -StartPolicy $vmStartup `
        -StartAction PowerOn -StartOrder $order 

}

foreach($esx in $hosts){
    Set-VMHostStartPolicy -VMHostStartPolicy $esx -Enabled:$true
    }

Disconnect-VIServer -Confirm:$false

 

I created a short movie about the script in action:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s