Advanced

Working with vCloud Metadata in PowerCLI

AlanFeb2012_thumb_thumb1_thumb_thumb[2]
Posted by
Alan Renouf
Technical Marketing

Yesterday William Lam created a great post which showed us how easy it was to work with vCloud Director (vCD) Metadata, he outlined how easy it was to add/update and remove metadata from the various objects which supported this function in vCD.

As he explained, we can use this to assign various useful information to our vCD objects, one of these was the contact information.  In this post I will show example scripts and how the code would look in PowerCLI when working with vCD Metadata.

In our example we will have a list of VMs starting with VMAlpha, these will be owned by Bob and will need to be marked in vCD with Bob as the owner, firstly lets list the VMs in PowerCLI:

SNAGHTML3c481d5c

Once we have listed these we can easily import the code from the scripts section below to give us three new PowerShell advanced functions which allow us to work with metadata. With these imported we can easily add new metadata to our VM objects, each piece of metadata has to have a unique key and a value, in this case the key will be “Owner” and the value will be Bob’s email address which is [email protected]”.

SNAGHTML3c4b7145

With this information added to these objects it is easy to list all VMs and any metadata which may exist on these objects, lets now list all our VMs and the metadata which belongs to them:

SNAGHTML3c4fa2e0

Or just the contact information:

SNAGHTML3c50c905

And of course we can very easily remove these items with the Remove-CIMetadata advanced function:

SNAGHTML3c534508

Metadata can be very useful for keeping data with the objects as they move around, and remember its not just VMs which can hold metadata, the following objects can be used with these functions:

Tenant (User) Provider (Admin)
Catalog
CatalogItem CatalogItem
Media Media
Network
Organization
Organization vDC
Provider vDC
vApp vApp
vAppTemplate vAppTemplate
Virtual Machine (treated as a vApp) Virtual Machine (treated as a vApp)

 

Metadata Functions

Paste and run the following functions into your window to allow you to perform the actions explained above.

Function New-CIMetaData {
    <#
    .SYNOPSIS
        Creates a Metadata Key/Value pair.
    .DESCRIPTION
        Creates a custom Metadata Key/Value pair on a specified vCloud object
    .PARAMETER  Key
        The name of the Metadata to be applied.
    .PARAMETER  Value
        The value of the Metadata to be applied.
    .PARAMETER  CIObject
        The object on which to apply the Metadata.
    .EXAMPLE
        PS C:\> New-CIMetadata -Key "Owner" -Value "Alan Renouf" -CIObject (Get-Org Org1)
    #>
     [CmdletBinding(
         SupportsShouldProcess=$true,
        ConfirmImpact="High"
    )]
    param(
        [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
            [PSObject[]]$CIObject,
            $Key,
            $Value
        )
    Process {
        Foreach ($Object in $CIObject) {
            $Metadata = New-Object VMware.VimAutomation.Cloud.Views.Metadata
            $Metadata.MetadataEntry = New-Object VMware.VimAutomation.Cloud.Views.MetadataEntry
            $Metadata.MetadataEntry[0].Key = $Key
            $Metadata.MetadataEntry[0].Value = $Value
            $Object.ExtensionData.CreateMetadata($Metadata)
            ($Object.ExtensionData.GetMetadata()).MetadataEntry | Where {$_.Key -eq $key } | Select @{N="CIObject";E={$Object.Name}}, Key, Value
        }
    }
}
Function Get-CIMetaData {
    <#
    .SYNOPSIS
        Retrieves all Metadata Key/Value pairs.
    .DESCRIPTION
        Retrieves all custom Metadata Key/Value pairs on a specified vCloud object
    .PARAMETER  CIObject
        The object on which to retrieve the Metadata.
    .PARAMETER  Key
        The key to retrieve.
    .EXAMPLE
        PS C:\> Get-CIMetadata -CIObject (Get-Org Org1)
    #>
    param(
        [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
            [PSObject[]]$CIObject,
            $Key
        )
    Process {
        Foreach ($Object in $CIObject) {
            If ($Key) {
                ($Object.ExtensionData.GetMetadata()).MetadataEntry | Where {$_.Key -eq $key } | Select @{N="CIObject";E={$Object.Name}}, Key, Value
            } Else {
                ($Object.ExtensionData.GetMetadata()).MetadataEntry | Select @{N="CIObject";E={$Object.Name}}, Key, Value
            }
        }
    }
}
Function Remove-CIMetaData {
    <#
    .SYNOPSIS
        Removes a Metadata Key/Value pair.
    .DESCRIPTION
        Removes a custom Metadata Key/Value pair on a specified vCloud object
    .PARAMETER  Key
        The name of the Metadata to be removed.
    .PARAMETER  CIObject
        The object on which to remove the Metadata.
    .EXAMPLE
        PS C:\> Remove-CIMetaData -CIObject (Get-Org Org1) -Key "Owner"
    #>
     [CmdletBinding(
         SupportsShouldProcess=$true,
        ConfirmImpact="High"
    )]
    param(
        [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
            [PSObject[]]$CIObject,
            $Key
        )
    Process {
        $CIObject | Foreach {
            $metadataValue = ($_.ExtensionData.GetMetadata()).GetMetaDataValue($Key)
            If($metadataValue) { $metadataValue.Delete() }
        }
    }
}

Note: In between writing these functions and posting this article I noticed that Clinton Kitson has also released some similar functions,  make sure you check out his functions and examples here.

Get notification of new blog postings and more by following VMware PowerCLI on Twitter: @PowerCLI