I recently needed to get a list of all vApp templates and their datastores within vCloud Director. I wasn’t able to easily get this information using the Get-* commandlets so I went with the Search-Cloud commandlet.
# Build a hash of arrays where the keys are the shadow VM's id and the values are arrays of the datastores where the shadow VMs live. $shadowsVMs = @{} search-cloud -querytype AdminShadowVM | % { if ( $shadowsVMs[$_.PrimaryVAppTemplate] -eq $null) { $shadowsVMs[$_.PrimaryVAppTemplate] = @($_.DatastoreName) } else { $shadowsVMs[$_.PrimaryVAppTemplate] += $_.DatastoreName } } # Build a hash where the keys are the org's id and the values are the org's name. This is used below to display a vApp's org. $orgNames = @{} search-cloud -querytype organization | % { $orgNames[$_.id] = $_.name } # Set the sort option $sortField = 'Org' # Use Search-Cloud to find all AdminVMs and filter to see if they are vApp Templates. This is the part I had a hard time with using the Get-* commandlets. search-cloud -querytype adminvm -property Container, ContainerName, Name, CatalogName, Org, DatastoreName ` -filter "IsVappTemplate==True" | ` select @{N='Org'; E={ $orgNames[$_.org] }}, ` @{N='Catalog'; E={ $_.CatalogName} }, ` @{N='vApp Template'; E={ $_.ContainerName} }, ` @{N='VM Name'; E={ $_.Name} }, ` @{N='Datastore'; E={ $_.DatastoreName }}, ` @{N='Shadow VMs'; E={ $shadowsVMs[$_.Container].count }}, ` @{N='Shadow VMs Datastores'; E={ $shadowsVMs[$_.Container] -join ', '}} ` | sort $sortField | ft -auto
If you’d rather send the output to a spreadsheet instead of the console, you can use export-csv on the last line:
# Build a hash of arrays where the keys are the shadow VM's id and the values are arrays of the datastores where the shadow VMs live. $shadowsVMs = @{} search-cloud -querytype AdminShadowVM | % { if ( $shadowsVMs[$_.PrimaryVAppTemplate] -eq $null) { $shadowsVMs[$_.PrimaryVAppTemplate] = @($_.DatastoreName) } else { $shadowsVMs[$_.PrimaryVAppTemplate] += $_.DatastoreName } } # Build a hash where the keys are the org's id and the values are the org's name. This is used below to display a vApp's org. $orgNames = @{} search-cloud -querytype organization | % { $orgNames[$_.id] = $_.name } # Set the sort option $sortField = 'Org' # Use Search-Cloud to find all AdminVMs and filter to see if they are vApp Templates. This is the part I had a hard time with using the Get-* commandlets. search-cloud -querytype adminvm -property Container, ContainerName, Name, CatalogName, Org, DatastoreName ` -filter "IsVappTemplate==True" | ` select @{N='Org'; E={ $orgNames[$_.org] }}, ` @{N='Catalog'; E={ $_.CatalogName} }, ` @{N='vApp Template'; E={ $_.ContainerName} }, ` @{N='VM Name'; E={ $_.Name} }, ` @{N='Datastore'; E={ $_.DatastoreName }}, ` @{N='Shadow VMs'; E={ $shadowsVMs[$_.Container].count }}, ` @{N='Shadow VMs Datastores'; E={ $shadowsVMs[$_.Container] -join ', '}} ` | export-csv templates.csv