Solving problems invented by others...
My journey to automate vCloud Director with PowerShell – part #11: create a vm from template

My journey to automate vCloud Director with PowerShell – part #11: create a vm from template

Now that finally all the setup is done, i want to create my first vm.
Fortunately this time PowerCLI has all the features i need to accomplish this task.

To create a new vApp from a template i could use the New-CIVApp commandlet. (https://code.vmware.com/docs/11794/cmdlet-reference/doc/New-CIVApp.html)

$myvapp = New-CIVApp -Name $testvappname -OrgVdc (Get-OrgVdc -Name $orgvdcname) -VAppTemplate $vapptemplatename

After the vApp has been created, i need to create add a existing OrgVDC network to the vApp. For this purpose the New-CIVAppNetwork commandlet could be used. (https://code.vmware.com/docs/11794/cmdlet-reference/doc/New-CIVAppNetwork.html)

New-CIVAppNetwork -Direct -ParentOrgVdcNetwork $myorgvdcnetwork -VApp $myvapp

After connecting the vApp to the OrgVDC network, i need to modify the nic of the vm and connect it to the newly created vApp network. For this purpose i found no PowerCLI commandlet. But luckily i already have some experiences in diving into the sub-objects of PowerCLI objects. I found that this settings could be fetched by the GetNetworkConnectionSetion() method in the ExtensionData sub-object of the vm.
You can just request the settings, modify it and write it back to the server.

# get the vm inside the vapp
$myvm = Get-CIVM -VApp $myvapp

# get the network configuration part of the vm object
$mynetworkconnectionsection = $myvm.ExtensionData.GetNetworkConnectionSection()

# since we assume this vm has only one nic we configure the parameters of the first nic -> index 0 of the array of nics
$mynetworkconnectionsection.NetworkConnection[0].IsConnected = $true # plug in the cable
$mynetworkconnectionsection.NetworkConnection[0].Network = $testnetworkname # define to which network
$mynetworkconnectionsection.NetworkConnection[0].IpAddressAllocationMode = "DHCP" # request dhcp ip allocation

# write back the changes to cloud director
$mynetworkconnectionsection.UpdateServerData()

The complete script:

# create new vapp from template
$myvapp = New-CIVApp -Name $testvappname -OrgVdc (Get-OrgVdc -Name $orgvdcname) -VAppTemplate $vapptemplatename

# get the OrgVDCNetwork to which the vapp should connect to
$myorgvdcnetwork = Get-OrgVdcNetwork -OrgVdc (Get-OrgVdc -Name $orgvdcname) -Name $testnetworkname

# add the OrgVDCNetwork to the vapp
New-CIVAppNetwork -Direct -ParentOrgVdcNetwork $myorgvdcnetwork -VApp $myvapp

# get the vm inside the vapp
$myvm = Get-CIVM -VApp $myvapp

# get the network configuration part of the vm object
$mynetworkconnectionsection = $myvm.ExtensionData.GetNetworkConnectionSection()

# since we assume this vm has only one nic we configure the parameters of the first nic -> index 0 of the array of nics
$mynetworkconnectionsection.NetworkConnection[0].IsConnected = $true # plug in the cable
$mynetworkconnectionsection.NetworkConnection[0].Network = $testnetworkname # define to which network
$mynetworkconnectionsection.NetworkConnection[0].IpAddressAllocationMode = "DHCP" # request dhcp ip allocation

# write back the changes to cloud director
$mynetworkconnectionsection.UpdateServerData()

Next article in this series:

3 Comments

  1. Hi,

    When i try and run the last command i get the below error. Any ideas why? I cant seem to figure it out:

    PS C:\Windows\system32> $mynetworkconnectionsection.UpdateServerData()
    Exception calling “UpdateServerData” with “0” argument(s): “[ f541eae2-8b05-48a8-8a62-261ca86f9020 ] No enum constant
    com.vmware.vcloud.api.rest.enums.IpAddressAllocationModeType.pool”
    At line:1 char:1
    + $mynetworkconnectionsection.UpdateServerData()
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CloudException

    Adam
  2. Hi Adam

    I can not find the cause of your error message. For me it looks like your system does not accept “DHCP” as IpAddressAllocationMode. Maybe DHCP is not activated on the parent OrgVdcNetwork or something like that?

    Sorry for not being really helpful.

    Regards
    Milla

    Michael
  3. Hi Adam

    I could not just forget your question. It came to my mind that i developed my script on a Cloud Director 10.0 with a NSX-V backend. Therefore i thought you might already have a newer environment.

    I did a quick test in our development environment which is already updated to Cloud Director 10.2 and NSX-T 3.1. Again i could not reproduce your error.

    Again i’m standing here without an answer.

    Sorry…

    Michael

Leave a Reply

Your email address will not be published.

twenty nine − twenty two =