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:
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 “Bob@corp.local”.
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:
Or just the contact information:
And of course we can very easily remove these items with the Remove-CIMetadata advanced function:
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
To run these sample scripts with vCloud Director 5.1, we’ll need to update the functions to accommodate a few changes.
For example, if you’re using the Get-CIMetaData function, you would update the process block to expand the TypedValue element and retrieve the Value:
Process {
Foreach ($Object in $CIObject) {
If ($Key) {
($Object.ExtensionData.GetMetadata()).MetadataEntry | Where {$_.Key -eq $key } | Select @{N="CIObject";E={$Object.Name}}, Key -ExpandProperty TypedValue
} Else {
($Object.ExtensionData.GetMetadata()).MetadataEntry | Select @{N="CIObject";E={$Object.Name}}, Key -ExpandProperty TypedValue
}
}
For more information, please review the Working With Object Metadata topic in the VMware vCloud Director 5.1 Documentation Center.
Old post I know, but potentially very useful. I’ve taken this code and updated it completely to support the Typed Value fields in later versions of vCloud Director. The updated module code is available on my blog at http://kiwicloud.ninja.
I am trying to put metadata value into a way to export to csv….although I am able to put the “value” out using something like….
$testmetadata = get-civm WADDC03 | get-cimetadata | Where { $_.key -eq “Class of Service”} | select ciobject, key, value
write-host $testmetadata.value
produces only the value…called Prod
which is what I want..
but if I try to use something like this to extract the value and try to use add-member I end up getting the result of @{Value=Prod}
get-civm |
Add-Member -Name “Class of Service” -Value { get-cimetadata $this | Where { $_.key -eq “Class of Service”} | select value } -MemberType ScriptProperty -Passthru -Force |
select name, status, “Class of Service”
I get this…
Name Status Class of Service
—- —— —————-
CLOUDTest01 PoweredOn @{Value=Test}
CLOUDTest04 PoweredOn @{Value=Test}
Testvmserver1 PoweredOn
ProdVMserver1 PoweredOn @{Value=Prod}
ProdVMserver2 PoweredOn @{Value=Prod}
Is there anyway to just get the value to show?