VMware Cloud Community
NEMike9
Contributor
Contributor
Jump to solution

How to enable "vApp Options" on an existing VM and add Properties that will be passed down via the OVF-ENV.XML



I am trying to figure out how from vSphere PowerCLI to enable the "vApp Options" on an exsiting VM (in this case a freshly cloned template) and then add Properties and enable cdrom access to OVF data such that when the VM is powered on the ovf-env.xml contains the properties in the cdrom drive.


If I use the vSphere client and open the VM's settings and select the "Options" tab and then select "vApp Options" and change the setting to Enabled and then select "vApp Options->Advanced" and click on the "Properties" button on the right I can add the Properties and then I can alter them from vSphere PowerCLI but since these actions aren't preserved if I clone the VM I need a way to set these up from vSphere PowerCLI without using the vSphere client to do it.


Thanks in advance for the help!

Mike

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can use the vSphere API to enable the "vApp Options" on an existing VM and set the properties. For example:

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.vAppConfig = New-Object VMware.Vim.VmConfigSpec

$spec.vAppConfig.property = New-Object VMware.Vim.VAppPropertySpec[] (1)

$spec.vAppConfig.property[0] = New-Object VMware.Vim.VAppPropertySpec

$spec.vAppConfig.property[0].operation = "add"

$spec.vAppConfig.property[0].info = New-Object VMware.Vim.VAppPropertyInfo

$spec.vAppConfig.property[0].info.key = 0

$spec.vAppConfig.property[0].info.classId = "Property1ClassID"

$spec.vAppConfig.property[0].info.instanceId = "Property1InstanceID"

$spec.vAppConfig.property[0].info.id = "Property_1"

$spec.vAppConfig.property[0].info.category = "Property1Category"

$spec.vAppConfig.property[0].info.label = "Property 1"

$spec.vAppConfig.property[0].info.type = "string"

$spec.vAppConfig.property[0].info.userConfigurable = $true

$spec.vAppConfig.property[0].info.defaultValue = "Property1DefaultValue"

$spec.vAppConfig.property[0].info.value = ""

$spec.vAppConfig.property[0].info.description = "Property1Description"

$vm = Get-VM -Name vm3

$vm.ExtensionData.ReconfigVM_Task($spec)

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
3 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can use the vSphere API to enable the "vApp Options" on an existing VM and set the properties. For example:

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.vAppConfig = New-Object VMware.Vim.VmConfigSpec

$spec.vAppConfig.property = New-Object VMware.Vim.VAppPropertySpec[] (1)

$spec.vAppConfig.property[0] = New-Object VMware.Vim.VAppPropertySpec

$spec.vAppConfig.property[0].operation = "add"

$spec.vAppConfig.property[0].info = New-Object VMware.Vim.VAppPropertyInfo

$spec.vAppConfig.property[0].info.key = 0

$spec.vAppConfig.property[0].info.classId = "Property1ClassID"

$spec.vAppConfig.property[0].info.instanceId = "Property1InstanceID"

$spec.vAppConfig.property[0].info.id = "Property_1"

$spec.vAppConfig.property[0].info.category = "Property1Category"

$spec.vAppConfig.property[0].info.label = "Property 1"

$spec.vAppConfig.property[0].info.type = "string"

$spec.vAppConfig.property[0].info.userConfigurable = $true

$spec.vAppConfig.property[0].info.defaultValue = "Property1DefaultValue"

$spec.vAppConfig.property[0].info.value = ""

$spec.vAppConfig.property[0].info.description = "Property1Description"

$vm = Get-VM -Name vm3

$vm.ExtensionData.ReconfigVM_Task($spec)

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
NEMike9
Contributor
Contributor
Jump to solution

Thanks your reply was very helpful, I've gotten a lot farther in my work...  I'm trying to write code to add several properties during one call to ReConfigVM, but I'm not sure I understand a few things including the: VMware.Vim.VAppPropertySpec[] (1)   does this mean to create an array with room for one entry or?

Here is my code, thanks again for your help!

function Add-A-Property($propLabel, $propType, $propValue)

{

    $newProp = New-Object VMWare.Vim.VAppPropertySpec

    $newProp.Operation = "edit"

    $prop = $myvm.ExtensionData.Config.VAppConfig.Property | Where {$_.Label -match $propLabel}

    if( ! $prop )

    {

        $prop = New-Object VMware.Vim.VAppPropertyInfo

        $newProp.Operation = "add"

    }

    $prop.Label = $propLabel

    $prop.Value = $propValue

    $prop.id = $propLabel

    $prop.type = $propType

    $prop.category = $propLabel

    $newProp.Info = $prop

    return $newProp

}

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.VAppConfig = New-Object VMWare.Vim.VmConfigSpec

if( ! $myvm.ExtensionData.Config.VAppConfig.Property )

{

    $spec.vAppConfig.Property = New-Object VMware.Vim.VAppPropertySpec[] (5)

}

$spec.VAppConfig.Property += Add-A-Property "HostName" "string" $Hostname

$spec.VAppConfig.Property += Add-A-Property "IPAddress" "ip" $IPv4

$spec.VAppConfig.Property += Add-A-Property "Netmask" "ip" $Netmask

$spec.VAppConfig.Property += Add-A-Property "Gateway" "ip" $Gateway

$spec.VAppConfig.Property += Add-A-Property "NameServers" "string" $NameServer

$myvm.ExtensionData.ReconfigVM($spec)

Here is the error I'm getting:

Exception calling "ReconfigVM" with "1" argument(s): "A specified parameter is not correct.

property.info.key[0]"

At C:\inetpub\powershell\vmware-setproperties.ps1:61 char:31

+ $myvm.ExtensionData.ReconfigVM <<<< ($spec)

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

0 Kudos
NEMike9
Contributor
Contributor
Jump to solution

I found the problem.  I need to set $prop.key = unique number for each property added.

0 Kudos