How can I use PowerCLI to quickly list vCloud Organizations, Org VDCs, VMs, and a VM's Metadata field called "Location"?
I'd like to do this using views, but I can't find information on how to do this, especially the metadata part.
Any ideas?
Getting Metadata info is simple.
Be sure you have imported the following module:
Import-Module Vmware.VimAutomation.Cloud
You need to connect to vcloud director with following command first:
Connect-CIServer -Server <yourvclouddirector> - user <yourusername> -password <yourpassword> -org <yourorganization>
Then you need to get VM information with Get-CIVM command like:
$vmv = Get-CIVM | Sort-Object -Property Name
Then you need to get Metadata information from VM with .ExtensionData.GetMetadata() property like:
Foreach ($vm $vmv) { $Metadata = $vm.ExtensionData.GetMetadata() }
If Metadata entry is not inserted before $Metadata.MetadataEntry.Count property is 0.
If not you can take the Metadata information Vlue from the field called Location Like this:
$MetaValue= $Metadata.MetadataEntry[[array]::indexof($Metadata.MetadataEntry.Key,"Location")].TypedValue.Value
So whole sctipt to get Metadata should be something like this: (Replace values of vclouddirector,username,password with yours)
Import-Module Vmware.VimAutomation.Cloud
Connect-CIServer -Server <yourvclouddirector> -user <yourusername> -password <yourpassword> -org <yourorganization>
$vmv = Get-CIVM | Sort-Object -Property Name
Foreach ($vm in $vmv) {
$Metadata = $vm.ExtensionData.GetMetadata()
$MetaValue= $Metadata.MetadataEntry[[array]::indexof($Metadata.MetadataEntry.Key,"Location")].TypedValue.Value
Write-Host $vm + $MetaValue
}
if ($global:DefaultCIServers.Count -gt 0) {Disconnect-CIServer -Server * -Force -Confirm:$false} #For disconnecting
Listing Organizations, VDCs and VMs should be straightforward using the standard Get-Org, Get-OrgVdc and Get-CIVM cmdlets.
Metadata is more of a challenge (as you say) as it's stored in VM extension data and not easily surfaced in PowerCLI. Alan Renouf wrote a blog post back in 2012 which included code for a PowerShell module to do this: Working with vCloud Metadata in PowerCLI - VMware PowerCLI Blog - VMware Blogs
The issue with this code is it was written before vCloud Director 5.x and doesn't support the typed values for metadata available in more recent releases. I took Alan's code and extended it for typed metadata values and it's available in a couple of blog posts I've written up:
Working with vCloud Metadata in PowerCLI – Part 1
Working with vCloud Metadata in PowerCLI – Part 2
Using this module you should be able to pipe the output from Get-Org | Get-OrgVdc | Get-CIVM into Get-CIMetaData with a -key "Location" filter.
Let me know how you get on and happy to assist further if I can.
Getting Metadata info is simple.
Be sure you have imported the following module:
Import-Module Vmware.VimAutomation.Cloud
You need to connect to vcloud director with following command first:
Connect-CIServer -Server <yourvclouddirector> - user <yourusername> -password <yourpassword> -org <yourorganization>
Then you need to get VM information with Get-CIVM command like:
$vmv = Get-CIVM | Sort-Object -Property Name
Then you need to get Metadata information from VM with .ExtensionData.GetMetadata() property like:
Foreach ($vm $vmv) { $Metadata = $vm.ExtensionData.GetMetadata() }
If Metadata entry is not inserted before $Metadata.MetadataEntry.Count property is 0.
If not you can take the Metadata information Vlue from the field called Location Like this:
$MetaValue= $Metadata.MetadataEntry[[array]::indexof($Metadata.MetadataEntry.Key,"Location")].TypedValue.Value
So whole sctipt to get Metadata should be something like this: (Replace values of vclouddirector,username,password with yours)
Import-Module Vmware.VimAutomation.Cloud
Connect-CIServer -Server <yourvclouddirector> -user <yourusername> -password <yourpassword> -org <yourorganization>
$vmv = Get-CIVM | Sort-Object -Property Name
Foreach ($vm in $vmv) {
$Metadata = $vm.ExtensionData.GetMetadata()
$MetaValue= $Metadata.MetadataEntry[[array]::indexof($Metadata.MetadataEntry.Key,"Location")].TypedValue.Value
Write-Host $vm + $MetaValue
}
if ($global:DefaultCIServers.Count -gt 0) {Disconnect-CIServer -Server * -Force -Confirm:$false} #For disconnecting