I would like to modify the code snippet below (http://kb.vmware.com/kb/1012514) to include the parent object.
get-view -ViewType HostSystem -Property Name, Config.Product | select Name,{$_.Config.Product.FullName},{$_.Config.Product.Build} | ft -auto
When I run the modified version I get "ComputeResource-domain-sNNNN" or "ClusterComputeResource-domain-cNNN" in the Parent column. If I do a
Get-Cluster ClusterComputeResource-domain-cNNN
I get the name of one of our clusters. So I tried variations of the following and I still can't get a proper Parent name.
PowerCLI C:\> get-view -ViewType HostSystem -Property Name, Config.Product, Parent | select Name,{$_.Config.Product.FullName},{$_.Config.Product.Build}, ( $_.Parent | get-cluster ) | sort -property name | ft -auto
select : Cannot convert System.Object[] to one of the following types {System.String, System.Management.Automation.ScriptBlock}.
At line:1 char:72
+ get-view -ViewType HostSystem -Property Name, Config.Product, Parent | select Na ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException
+ FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand
PowerCLI C:\>
I realize that it doesn't help that in some cases Parent will be a cluster object and other cases it will be a Folder or Data Center object. I have also tried
.... $( if ( $_.Parent) {get-cluster -id $_.Parent} )
.... ( if ( $_.Parent) {get-cluster -id $_.Parent} )
.... ( get-cluster -name ($_.Parent).Name )
.... ( get-cluster -name [string]($_.Parent).Name )
1) If I only care about hosts with cluster parents, is there a way to ignore all others?
2) How do I get the name of the parent cluster?
Try like this
Get-View -ViewType HostSystem -Property Name, Config.Product,Parent |
select Name,
@{N='Product';E={$_.Config.Product.FullName}},
@{N='Build';E={$_.Config.Product.Build}},
@{N='Cluster';E={
$parent = Get-View -Id $_.Parent -Property Name,Parent
While ($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent.Parent){
$parent = Get-View -Id $parent.Parent -Property Name,Parent
}
if($parent -is [VMware.Vim.ClusterComputeResource]){
$parent.Name}}} |
Format-Table -auto
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
Get-View -ViewType HostSystem -Property Name, Config.Product,Parent |
select Name,
@{N='Product';E={$_.Config.Product.FullName}},
@{N='Build';E={$_.Config.Product.Build}},
@{N='Cluster';E={
$parent = Get-View -Id $_.Parent -Property Name,Parent
While ($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent.Parent){
$parent = Get-View -Id $parent.Parent -Property Name,Parent
}
if($parent -is [VMware.Vim.ClusterComputeResource]){
$parent.Name}}} |
Format-Table -auto
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc:
That's awesome. Thanks.
I would like to understand how you came up with this solution as I'm still learning PowerShell and PowerCLI. For the line that starts "$parent = Get-View", why use Get-View instead of Get-Cluster?
Sure, the Parent property contains what is called a MoRef or ManagedObjectReference, in other words a pointer to the actual parent object.
You can convert a MoRef to the actual object with the Get-View cmdlet and the Id parameter.
The scripts keeps folllowing the Parent trail until it finds a Cluster object or until the Parent property contains $null (for stand-alone ESXi nodes).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc:
What is the purpose of the While loop? Why not use If?
The While loop follows the Parent chain until a cluster is encountered or until there is no parent (standalone ESXi node)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc:
Right. I'm just noticing the "$parent.Parent" vs. "$_.Parent " above it.
That clears things up.
Thanks.