PowerCLI: Remap vCD Network When Duplicate Exists

Ok so it’s a bit of a long title.

When you import a virtual machine into vCloud Director from the underlying vCenter Server you can remap the network as mentioned in my previous blog post Import VMs from vCenter to vCloud Director using PowerCLI.
This works great up to the point when you have more than one vApp network with the same name, something that grows increasingly likely the bigger your vCD environment gets.

There is a way around this issue.  You can get PowerCLI to query the vApp that you are trying to connect the imported VM to and use the output of the vApp network name in the remap command.  Let me show you.

First connect PowerCLI to the vCD cell and the underlying vCenter.

$ciserver = vclouddirector.domain.com
$viserver = vcenter.domain.com

Connect-VIServer -Server $viserver
Connect-CIServer -Server $ciserver

Now specify the vApp name

$civapp = vApp_Name

Then specify the vApp network name so you can call it when you get the VM to do the remap.

$cinetwork = get-civapp $civapp | Get-CIVAppNetwork Network_Name

Finally run the remap command

Get-CINetworkAdapter | Set-CINetworkAdapter -vappnetwork $cinetwork -IPaddressAllocationMode Pool -Connected $True

To simplify this further you can run this all as part of a script to query all the VMs that are added to the vApp and remap all their networks to use the same one.  To use this just copy and paste it into a text editor and save it as a .PS1 file.

$ciserver = 'vclouddirector.domain.com'
$viserver = 'vcenter.domain.com'

Connect-VIServer -Server $viserver
Connect-CIServer -Server $ciserver

<$civapp = 'vApp_Name'
$cinetwork = get-civapp $civapp | Get-CIVAppNetwork 'Network_Name'
foreach ($civm in $civms) {
$civm | Get-CINetworkAdapter | Set-CINetworkAdapter -vappnetwork $cinetwork -IPaddressAllocationMode Pool -Connected $True

You can add an extra layer to this by specifying a variable to query Organisation Virtual Datacenter first then add this to the $cinetwork variable as follows.

$orgvdc = OrgvDC_Name
$cinetwork = get-orgvdc $orgvdc | get-civapp $civapp | Get-CIVAppNetwork Network_Name

Leave a Reply

Your email address will not be published. Required fields are marked *