Set vApp Guest Shutdown

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)

10 thoughts on “Set vApp Guest Shutdown”

  1. 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

  2. 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

  3. Saved me a whole lot of mouseclicks, thanks for that!

    However should you stumble upon an empty vApp like I did, the script will fail for it tries to work on an empty argument. So I merely added an extra line of code.

    $VC = Connect-VIServer “MyvCenter.local”

    ForEach ($Vapp in Get-VApp)
    {

    # If vApp contains no VMs the script will skip this vApp
    If ($VApp.ExtensionData.ChildConfiguration.Count -ne 0)

    {
    $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

    »crosslinked«

  4. Script saved me a whole lot of mouseclickc, thanks for that. However if you would stumble upon an empty vApp the script would fail. For that I merely added an extra line of code preventing the script from checking on an empty $Entity.

    $VC = Connect-VIServer “MyvCenter.local”

    ForEach ($Vapp in Get-VApp)
    {

    # If VApp has no VMs the script will skip this VApp
    If ($VApp.ExtensionData.Vm.Count -ne 0)

    {
    $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

  5. Hello,

    I was trying to get the list of virtual appliances installed on vCenter Server. Can you please help me with that.

  6. Hi tata,

    You can simply connect to vcenter (look at the first line of the script) and next use Get-vApp to retrieve a list.

    Regards,
    Hugo

  7. If you have vApps inside of vApps this will set their startup to an invalid state. To fix this, just make sure the EntityConfig you are changing is a VirtualMachine and not a VirtualApp.

    Change the line
    If ($Entity.StopAction -ne “guestShutdown”)
    to
    If ($Entity.StopAction -ne “guestShutdown” -and $Entity.Key.Type -eq “VirtualMachine”)

Leave a Reply