powercli

Modifying vApp Properties with PowerShell and PowerCLI

One of my goals for this year has been to automate deployment and configuration of VMware Horizon Workspace. As part of this effort, I found it difficult to understand how to change vApp properties through PowerCLI.

Based on my findings, I created the function below to make it a lot easier for other people looking to do the same.

#Configure Necessary vApp Settings
function Modify-vApp-Properties($myvapp, $propertylabel, $propertyvalue)
{
	$myvapp
	$propertylabel
	$propertyvalue

    $myview = $myvapp | Get-View 
	$myvappproperties = $myvapp.ExtensionData.VAppConfig.Property
	$myprop = $myvappproperties | Where {$_.Label -match $propertylabel}
	$myprop.Value = $propertyvalue

	$spec = New-Object VMware.Vim.vAppConfigSpec
	$newprop = New-Object VMware.Vim.VAppPropertySpec
	$newProp.Operation = "edit"
	$newProp.info = $myprop
	$spec.Property += $newProp

	$myview.UpdateVAppConfig($spec)
}

#Sample on how to use it
$myhwsvapp = Get-VApp | Where {$_.name -match "NTNX-VDIXP-HWS"} 
Modify-vApp-Properties -myvapp $myhwsvapp -propertylabel "configurator-va Network 1 IP Address" -propertyvalue "172.16.1.7"

To change a vApp property using PowerShell/PowerCLI, you basically need to build a “VMware.Vim.vAppConfigSpec” object with an associated “VMware.Vim.VAppPropertySpec” object, then pass the vAppConfigSpec object into the UpdateVappConfig method.

In this scenario, I just want to change the value of a specific vApp property with label “configurator-va Network 1 IP Address”.

In this code segment I copy the existing ExtensionData.vAppConfig.Property object, and then change the value to the desired value:

$myvappproperties = $myvapp.ExtensionData.VAppConfig.Property
$myprop = $myvappproperties | Where {$_.Label -match $propertylabel}
$myprop.Value = $propertyvalue

Then we build the new spec that we can use with the modified property object, and update the vApp properties with the UpdateVappConfig method:

$spec = New-Object VMware.Vim.vAppConfigSpec
$newprop = New-Object VMware.Vim.VAppPropertySpec
$newProp.Operation = "edit"
$newProp.info = $myprop
$spec.Property += $newProp

$myview.UpdateVAppConfig($spec)

At Nutanix, we are working on new ways to make deploying EUC technologies such as VMware Horizon Suite and Citrix XenDesktop very easy for our customers. Since a Nutanix block consists of all the compute, virtualization, and storage components, we have a unique capability to expose hooks into everything that a customer needs to do some really cool automation.  On our near-term roadmap for the 3.5 release is a REST-based API that can be used with PowerShell and its Invoke-RestMethod, ConvertTo-Json, and ConvertFrom-Json cmdlets.

Hoping this code will help someone else out there looking to change vApp properties with PowerCli/PowerShell. If you have any better methods, please comment below!

Cheers,
Lukas

Leave A Comment