Relating vCloud Director to vCenter in PowerCLI

I was listening to episode 216 of the PowerScripting Podcast recently, Hal and Jonathan were talking to vCloud Director (vCD) expert Jake Robinson and “meeting expert” (listen it will make sense) Damian Karlson, it was a great show, very funny and I highly recommend you listen here: http://powerscripting.wordpress.com/2013/02/26/episode-216-jake-robinson-and-damian-karlson-talk-powercli/

Anyway, they were talking about relating vCD objects to vCenter objects, on the show they said it couldn’t be done without matching an ID and writing a custom function, this used to be the case but now I wanted to show a few cool things from PowerCLI which allows us to use a parameter called –RelatedObject to take away all the hard work from the matching IDs, Datastore IDs, Network IDs etc.

Word of warning!

Just so you are aware, whilst VMware do provide a method for relating objects between vCD and vSphere go careful where you use it, whilst you would theoretically be able to get a cloud VM and relate this to a vSphere VM and then go on in your script to alter the VM or do some other kind of action on it I would not do this.  vCD needs to know about the changes you make or you may get strange unsupported results.  Generally I would stick to using this for cool reporting scripts.

RelatedObject Parameter

With the RelatedObject parameter on some of the PowerCLI cmdlets, you can retrieve vSphere inventory objects and vSphere PowerCLI view objects from cloud resources, what’s more is that we even let you pipeline these cmdlets together to get the results. The use of these cmdlets allowing you to relate the information between the two applications allows for automation, reporting, and troubleshooting options for provider administrators and gives us an easy way to retrieve data from both applications in the same interface.  Exactly what PowerShell was designed for!

The list of cmdlets which you can use with this parameter can be seen below, you will also notice that this parameter exists in the VMware Distributed Switch snapin as well:

image

Of course for these to work you will need to connect to vCenter with the Connect-VIServer cmdlet and in the same session connect to the vCloud Director session with Connect-CIServer, bear in mind that you may have multiple vCenters backing your vCD environment so a connection will need to be made to all of these.

Retrieving vSphere Objects from vCloud Director Resources

Provider administrators can use the RelatedObject parameter of vSphere PowerCLI cmdlets to retrieve vSphere inventory objects from vCloud Director objects. Passing the retrieved objects to the cmdlets of the VMware.VimAutomation.Core and VMware.VimAutomation.VDS snap-ins easily allows for the related object to be returned.

The following table shows which objects are currently supported:

Cloud Object Retrieved vSphere Inventory Object Example code
ProviderVdc Datastore Get-ProviderVdc -Name ‘MyProviderVdc’ | Get-Datastore
CIVM VirtualMachine Get-CIVM -Name ‘MyCloudVM’ | Get-VM
NetworkPool VDSwitch Get-NetworkPool -Name ‘MyNetworkPool’ | Get-VDSwitch
NetworkPool VDPortgroup Get-NetworkPool -Name ‘MyNetworkPool’ | Get-VDPortGroup
ExternalNetwork VDPortgroup Get-ExternalNetwork -Name ‘MyExternalNetwork’ | Get-VDPortGroup

Here is an example of this working:

Get-CIVM | Get-VM

Retrieving vSphere Views from vCloud Director Views

For the administrators with advanced knowledge and understanding of .NET Framework, vSphere PowerCLI, PowerShell scripting, and vSphere and vCloud APIs or if you are familiar with using the Get-View Cmdlet and Get-CIView Cmdlet you can retrieve vSphere PowerCLI views from vCloud Director PowerCLI views with the Get-CIView and Get-View cmdlets.

The following table shows which supported vSphere views you can retrieve from views:

vCloud Director view object vSphere view object Example code
VMWExternalNetwork DistributedVirtualPortGroup Get-ExternalNetwork -Name ‘MyExternalNetwork’ | Get-CIView | Get-View
VLanPool DistributedVirtualSwitch Get-NetworkPool -Name ‘MyVlanPool’ | Get-CIView | Get-View
FencePool DistributedVirtualSwitch Get-NetworkPool -Name ‘MyFencePool’ | Get-CIView | Get-View
VimServer ServiceInstance $providerVdcView = Get-ProviderVdc -Name ‘MyProviderVdc’ | Get-CIViewGet-CIView -Id $providerVdcView.VimServer[0].Href | Get-View
VMWProviderVdcResourcePool ResourcePool $providerVdcView = Get-ProviderVdc -Name ‘MyProviderVdc’ | Get-CIView $resourcePoolSet = $providerVdcView.GetResourcePools() $resourcePoolSet.VMWProviderVdcResourcePool | Get-View
Datastore Datastore Get-CIDatastore -Name ‘MyDatastore’ -ProviderVdc ‘MyProviderVdc’ | Get-CIView | Get-View
CIVM VirtualMachine Get-CIVM -Name ‘MyVM’ | Get-CIView | Get-View

Here are a few examples of this working:

Get-View Example

-Alan

Alan

Alan Renouf has a role of Automation Frameworks Product Manager at VMware responsible for providing the architects and operators of the cloud infrastructure with the toolkits/frameworks and command-line interfaces they require to build a fully automated software-defined datacenter. Alan is a frequent blogger at http://blogs.vmware.com/vipowershell and has a personal blog at http://virtu-al.net. You can follow Alan on twitter as @alanrenouf.

You may also like...

2 Responses

  1. Clark,

    Did you ever figure this out?

    JD

  2. cupdike says:

    Using PowerCLI I am trying to add a VM to a vCloud vApp using a vCloud Template. But there is no New-CIVM (just New-VM) so I don’t know how to do this due to the vCloud objects != vCenter objects. Naively trying to use a vCloud template and a vCloud vApp gets this error:

    New-VM : Cannot bind parameter ‘Template’. Cannot convert the “myvapptemplate” value of type “VMware.VimAutomation.Cloud.Impl.V1.CIVAppTemplateImpl”
    to type “VMware.VimAutomation.ViCore.Types.V1.Inventory.Template”.

    Can you explain how this can be done?

    Thanks,
    Clark

Leave a Reply

%d bloggers like this: