Annoyed by the default setting for VM’s inside a vApp? I was, because when you power down the vApp, the VM’s are powered down instead of being shutdown cleanly. It can be a tedious task to check and correct this setting for all your vApps. This little script solves that problem for you. Enjoy!

$VC = Connect-VIServer "MyvCenter.local"
 
ForEach ($Vapp in Get-VApp)
{	
	$VAppView = $VApp | Get-View
	ForEach ($Entity in $VAppView.VAppConfig.EntityConfig)
	{
		If ($Entity.StopAction -ne "guestShutdown")
		{
		$VAppConfigSpec = New-Object VMware.Vim.VAppConfigSpec
		$EntityConfig = New-Object VMware.Vim.VAppEntityConfigInfo
			$EntityConfig.Key = (Get-View $Entity.Key).MoRef
			$EntityConfig.StopAction = "guestShutdown"
		$VAppConfigSpec.EntityConfig = $EntityConfig
 
		$VAppView.UpdateVAppConfig($VAppConfigSpec)
		}
	}
}
 
Disconnect-VIServer -Confirm:$false

Download script: Set-VAppGuestShutdown (rename to .ps1)

Tagged with:
 

4 Responses to Set vApp Guest Shutdown

  1. Dan says:

    Thanks,
    Works perfect.

  2. Robin says:

    This works perfectly. One question, if I wanted to set the StopAction, or in mycase StartAction for a particular VM in a VApp, how would I go about it. I can see how you set them all, just can’t see how to select a specific VM in the VApp. Thanks

  3. admin says:

    Robin,
    The easiest way is like this (note the “myVMName”). If you are using it in a larger script you can reference the vm’s by MoRef, but I will skip that explanation for now.

    $VC = Connect-VIServer "MyvCenter.local"
     
    ForEach ($Vapp in Get-VApp)
    {	
    	$VAppView = $VApp | Get-View
    	ForEach ($Entity in ($VAppView.VAppConfig.EntityConfig | Where {$_.tag -eq "MyVMName"}))
    	{
    		If ($Entity.StopAction -ne "guestShutdown")
    		{
    		$VAppConfigSpec = New-Object VMware.Vim.VAppConfigSpec
    		$EntityConfig = New-Object VMware.Vim.VAppEntityConfigInfo
    			$EntityConfig.Key = (Get-View $Entity.Key).MoRef
    			$EntityConfig.StopAction = "guestShutdown"
    		$VAppConfigSpec.EntityConfig = $EntityConfig
     
    		$VAppView.UpdateVAppConfig($VAppConfigSpec)
    		}
    	}
    }
     
    Disconnect-VIServer -Confirm:$false

    Hugo

  4. Loren says:

    Just got bit by this! Thanks for the script!