Relating vCloud/vCenter Objects

It's easy to go from vCloud to vCenter: link

Not so much from vCenter to vCloud...

Here's a function I came upon in the #vmware channel on freenode to relate vCenter VMs to vCloud VMs using PowerCLI:

function Get-CIVMfromVM {  
    <#
        .SYNOPSIS
            Converts a vCenter VM Object into a vCloud VM

        .DESCRIPTION
           Converts a vCenter VM Object into a vCloud VM

        .PARAMETER  VM
            One or more vCenter VMs

        .EXAMPLE
            PS C:\> Get-VM | Get-CIVMfromVM
    #>

    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline=$true)]
        $VM
    )

    process {
        If ((-not $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 ($SingleVM in $VM) {
            $MoRef = $SingleVM.ExtensionData.MoRef.Value
            $vCenterInstanceUUID = ($DefaultVIServers | Where-Object {$_.Name -eq $SingleVM.Uid.Split('@')[1].Split(':')[0]}).InstanceUuid
            Get-CIVM |
            Where-Object {
              $_.ExtensionData.VCloudExtension[0].Any[0].VmVimObjectRef.MoRef -eq $MoRef -and (Get-CIView -Id $_.Extensiondata.vCloudExtension[0].Any[0].VmVimObjectRef.VimServerRef.href).Uuid -eq $vCenterInstanceUUID
            }
        }
    }
}

It seems to do the trick, albeit slowly.

Tagged in: VMware, vCloud, PowerShell
comments powered by Disqus