The greatest challenge to any thinker is stating the problem in a way that will allow a solution

Bertrand Russell

By

On 2 Feb 2012

PowerCLI, VMware

Tags: , ,


vApps can be wonderful things.  Essentially resource pools on steroids, I never really used resource pools that much (unless I was revising for a VCP exam), but I find the ability to control a bunch of VM’s as one entity, controlling their start-up and shut-down order very useful.  However, controlling that start-up and shut-down functionality is a pain.  The default options are (for me) wrong, and its a laborious task to go through them all and update.

As soon as I feel the potential for RSI in my mouse arm, is when I feel the need for a bit of PowerCLI script-o-magic…

Before we get to the script its probably worth reviewing a few of the oddities of vApp power operations.  When dealing with VM’s with VM Tools installed properly, you always use Guest Shutdown (rather than Power Off) so that the VM’s OS gets an opportunity to shut-down gracefully.  With a vApp you use Power Off, this instructs a vApp to shut-down its VM’s in the order and manner prescribed in its settings.

Another thing to be aware of that is that when VM’s shutdown, the operation can time-out if the OS hasn’t shut-down within the window in the Shutdown Action.  Yes, the GUI and documentation suggest that it will just carry on with the next VM’s, this isn’t always the case.  But, on numerous occasions I’ve had vApp shut-downs come to halt because its timed out waiting for a VM to shut-down, and times-out the whole vApp shut-down.  I assume this is a bug, when and if I get to understand more, I’ll post it.

The following script applies the same settings to all VM’s, and is normally good for 95% of all VM’s, the odd exception you can always alter by hand.  For the reason above its worth giving machines that are slow to shut-down (MS Exchange servers come to mind) plenty of time to complete shutting down.

$vAppName = "MyvApp"
 
$StartDelay = 120
$StartVMToolsReady = $true
 
$StopDelay = 120
$StopMethod = "guestShutdown"
#$StopMethod = "powerOff"
#$StopMethod = "suspend"
#$StopMethod = "none"
 
# Get view of vApp
$vAppView = Get-View (Get-vApp $vAppName)
 
# Prep reconfig object
$vAppConfigSpec = New-Object VMware.Vim.VAppConfigSpec
$vAppConfigSpec.entityConfig = New-Object VMware.Vim.VAppEntityConfigInfo[] ($vAppView.vAppConfig.EntityConfig.Count)
 
# Go through each child and check config is as we wish
$i = 0
foreach ($vm in $vAppView.vAppConfig.EntityConfig) {
    $vAppConfigSpec.entityConfig[$i] = New-Object VMware.Vim.VAppEntityConfigInfo
    $vAppConfigSpec.entityConfig[$i].key = $vm.Key
    $vAppConfigSpec.entityConfig[$i].tag = $vm.Tag
    $vAppConfigSpec.entityConfig[$i].startDelay = $StartDelay
    $vAppConfigSpec.entityConfig[$i].waitingForGuest = $true
    $vAppConfigSpec.entityConfig[$i].stopDelay = $StopDelay
    $vAppConfigSpec.entityConfig[$i].stopAction = $StopMethod
    $i ++
}
 
$vAppView.UpdateVAppConfig($vAppConfigSpec)

 


Leave a Reply

XHTML: You can use these tags if you know what they are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.