Advanced

Linking your vCloud Director VMs and vSphere VMs

AlanFeb2012_thumb_thumb1_thumb
Posted by
Alan Renouf
Technical Marketing

One of PowerShell’s great strengths is its ability to take the output of one cmdlet and pipe it into another object, this enables you to create powerfull one line scripts which can be used to change multiple machines very easily.  PowerCLI is no exception, with a single line of code we can already add a portgroup to multiple hosts, create thousands of VMs or disconnect CD-ROMs from hundreds of VMs which would otherwise take days or hours in the vSphere Client.

With the introduction of PowerCLI 5.0.1 we now have multiple cmdlets to manage our vCD environment, but have you ever tried piping the vCD cmdlets into a vSphere PowerCLI cmdlet ?

Lets see what happens?

Firstly we can easily retrieve a Cloud VM object which is part of a particular vApp by using the following cmdlets together:

PS C:\> Get-CIVApp DOMAIN01 | Get-CIVM DOMCON | Format-List

ExtensionData   : VMware.VimAutomation.Cloud.Views.Vm
Status          : PoweredOff
Deleted         : False
GuestOsFullName : Red Hat Enterprise Linux 5 (32-bit)
CpuCount        : 1
MemoryMB        : 256
VMVersion       : v8
Org             : Tenant01
OrgVdc          : Bronze
VApp            : Domain01
Description     :
Href            :
https://vCloud/api/vApp/vm-4b2446a8-b3da-415e-adab-d5e85887ad71
Id              : urn:vcloud:vm:4b2446a8-b3da-415e-adab-d5e85887ad71
Name            : DOMCON

Now lets add in a PowerCLI cmdlet and see what happens, for example, lets try and see what Network adapter and the details for this adapter using the PowerCLI (not vCD PowerCLI) cmdlet called Get-NetworkAdapter.  For this to work you must use the Connect-CIServer cmdlet to first connect to your cloud infrastructure and then the Connect-VIServer cmdlet to connect to any vCenter instances this cloud uses, these connections will need to be in the same session.

PS C:\> Get-CIVApp DOMAIN01 | Get-CIVM DOMCON | Get-NetworkAdapter


Get-NetworkAdapter : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:59 + Get-CIVApp DOMAIN01 | Get-CIVM DOMCON | Get-NetworkAdapter <<<< + CategoryInfo          : InvalidArgument: (DOMCON:PSObject) [Get-NetworkAdapter], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetNetworkAdapter

As you can see, the Get-NetworkAdapter wasn’t written to understand what the vCD VM object looked like and therefore was unable to display the information.

What can we do ?

Recently there was a couple of great posts by a colleague of mine William Lam, in them he showed how a VM could be linked back to the vCenter it was being hosted on based on various properties which belong to the vCloud object, these posts are well worth a read and can be found here:

So with the information from these posts and knowing with PowerShell it is easy to wrap this in a nice function we can do just that.  Below you will find a function which is called Convert-ToVM which takes the vCloud VM Object and converts it to a vSphere VM, this will then open up all cmdlets which can currently be used against a vSphere VM object to the Cloud VMs.

Warning:  Just as it is not supported to alter or perform actions through the vSphere Client on your vCD hosted VMs, this function should be used with the same considerations in mind.  If you are in any doubt then please check with VMware Support before using this to perform bulk operations on your vCD VMs.

Function Convert-ToVM {
    <#
        .SYNOPSIS
            Converts a vCloud VM into a vCenter VM Object

        .DESCRIPTION
            Converts a vCloud VM into a vCenter VM Object

        .PARAMETER  CIVM
            One or more vCloud VMs
           
        .EXAMPLE
            PS C:\> Get-CIVM | Convert-ToVM
    #>
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline=$true)]
        $CIVM
    )
    process {
        If (-not $Runonce) {
            Write-Warning "Convert-ToVM should be used for view purposes only, using cmdlets which alter the vSphere VM in any way is not supported and may cause issues.`n"
            $Runonce = $true
        }
        If ((-no
t $DefaultCIServers) -or (-not $DefaultVIServer)){
            Write-Host -ForegroundColor Red "You will need to be connected to both the CIServer and VIServer for this cmdlet to work"
            Return
        }
        Foreach ($VM in $CIVM) { 
            $vsphereVMView = Get-View –RelatedObject $vm.ExtensionData
            Get-VIObjectByVIView $vsphereVMView
        }
    }
}

So now we have this handy function lets see how we can use our previous example to get the network adapter information:

PS C:\> Get-CIVApp DOMAIN01 | Get-CIVM DOMCON | Convert-ToVM | Get-NetworkAdapter | Format-List
WARNING: Convert-ToVM should be used for view purposes only, using cmdlets which alter the vSphere VM in any way is not supported and may cause issues.

MacAddress       : 00:50:56:01:00:05
WakeOnLanEnabled : True
NetworkName      : none
Type             : Flexible
ParentId         : VirtualMachine-vm-97
Parent           : DOMCON (4b2446a8-b3da-415e-adab-d5e85887ad71)
Uid              : /[email protected]:443/VirtualMachine=VirtualMachine-vm-97/NetworkAdapter=4000/
ConnectionState  : NotConnected, GuestControl, NoStartConnected
ExtensionData    : VMware.Vim.VirtualPCNet32
Id               : VirtualMachine-vm-97/4000
Name             : Network adapter 1

Another example is where we might want to retrieve the performance statistics of one of our vCloud VMs, lets concentrate on the CPU average usage:

PS C:\> Get-CIVM DOMCON | Convert-ToVM | Get-Stat -stat cpu.usage.average

MetricId                Timestamp                          Value Unit
——–                ———                          —– —-
cpu.usage.average       3/10/2012 12:00:00 AM               1.86 %
cpu.usage.average       3/9/2012 12:00:00 AM                1.83 %
cpu.usage.average       3/8/2012 12:00:00 AM                1.83 %
cpu.usage.average       3/7/2012 12:00:00 AM                1.85 %
cpu.usage.average       3/6/2012 12:00:00 AM                1.86 %
cpu.usage.average       3/5/2012 12:00:00 AM                1.82 %
cpu.usage.average       3/4/2012 12:00:00 AM                1.71 %
cpu.usage.average       3/3/2012 12:00:00 AM                 1.7 %
cpu.usage.average       3/2/2012 12:00:00 AM                1.71 %
cpu.usage.average       3/1/2012 12:00:00 AM                1.71 %
cpu.usage.average       2/29/2012 12:00:00 AM               1.71 %
cpu.usage.average       2/28/2012 12:00:00 AM               1.75 %
cpu.usage.average       2/27/2012 12:00:00 AM                  2 %
cpu.usage.average       2/26/2012 12:00:00 AM               1.83 %
cpu.usage.average       2/25/2012 12:00:00 AM               1.94 %
cpu.usage.average       2/24/2012 12:00:00 AM               2.11 %
cpu.usage.average       2/23/2012 12:00:00 AM               1.83 %

Summary

In summary, using these cmdlets together can give you information which you may not be able to retrieve by other means, but this must be used with extreme caution and any write or change operations must be avoided unless otherwise cleared with VMware support.