lanwench
Enthusiast
Enthusiast

Using get-view for a clustercomputeresource, what's the fastest way to get the datacenter it's in?

Jump to solution

Hi all -

If I use get-view to pull up a vmhost object, I can easily find the cluster it's in by checking the parent.

If I use get-view to pull up a vmhost cluster object, the parent is a folder, not a datacenter.

What's the quickest way to grab the datacenter name for a cluster object? I've been beating my head into a wall and must be missing something....

Thanks!

Paula

(PowerCLI v6.5)

typo!

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

No, there are hidden folders in play I'm afraid.

From a datacenter you can have 4 types of folders (Host,VM,Storage,Network).

But these are all under a hidden folder with the respective names host,vm,storage and network.

So it becomes datacenter -> host -> cluster

On the loop, in the best situation there is only 1 run through the loop.

In fact if you never have a folder to organise your clusters under the datacenter, you could do (which is fact an implicite 1 run through loop)

$cl = Get-View -ViewType ClusterComputeResource -Property Name,Parent

$dc = Get-View -Id (Get-view -Id $cl.Parent -Property Name,Parent).Parent -Property Name

$dc.Name

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership

Try something like this.

It follows the Parent chain until it finds a Datacenter

Get-View -ViewType ClusterComputeResource -Property Name,Parent |

Select Name,

@{N='Datacenter';E={

    $parent = Get-View -Id $_.Parent -Property Name,Parent

    while($parent -isnot [VMware.Vim.Datacenter]){

        $parent = Get-View -Id $parent.Parent -Property Name,Parent

    }

    $parent.Name

}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

PabloJAguilar20
Enthusiast
Enthusiast

you tried with that?

for get cluster from host  =  get-vmhost $vmhost | get-cluster

for get datacenter from host = get-vmhost $vmhost | get-datacenter

Regards

Pablo J Aguilar Consultor de Infraestructuras Virtuales. Buenos Aires - Argentina
0 Kudos
lanwench
Enthusiast
Enthusiast

Hi Luc - thanks, as always!

I was hoping there was something faster than a loop...I've used that method before.

The ultimate goal is to create two hashtables for lookups - one for hosts --> clusters, and one for clusters->datacenters.

This part works fine -

$VMhosts = Get-View -ViewType HostSystem -Property Name,Parent

$Clusters = Get-View -viewtype ClusterComputeResource -Property Name,Parent

                   

$HostsHash = @{}

$ClustersHash = @{}

$HoststoClusterHash = @{}

$Clusters | Foreach-Object {$ClustersHash.Add($_.moref.toString(),$_.name)}

$VMhosts | Foreach-Object {$HostsHash.Add($_.moref.toString(),$_.name)}

$VMhosts | Foreach-Object {$HostsToClusterHash.add($_.moref.toString(),$ClustersHash.($_.Parent.ToString()))}

I was hoping I could do something similar with datacenters and avoid having to do more get-view looping.. and the following obviously does not work.

$Datacenters = Get-view -ViewType Datacenter -Property Name

$DatacentersHash = @{}

$ClustersToDatacenterHash = @{}

$Datacenters | Foreach-Object {$DatacentersHash.Add($_.moref.toString(),$_.name)}

$Clusters | Foreach-Object {$ClustersToDatacenterHash.add($_.moref.toString(),$DatacentersHash.(magical_invisible_property)}

It seems like the parent for a cluster ought to be a datacenter, but I may have missed that day in biology class.

If it ain't possible, it ain't possible. I'll cope!

0 Kudos
lanwench
Enthusiast
Enthusiast

Thank you, Pablo - I'm using get-view in the interest of speed (and also to try & learn new things Smiley Happy  )

0 Kudos
LucD
Leadership
Leadership

No, there are hidden folders in play I'm afraid.

From a datacenter you can have 4 types of folders (Host,VM,Storage,Network).

But these are all under a hidden folder with the respective names host,vm,storage and network.

So it becomes datacenter -> host -> cluster

On the loop, in the best situation there is only 1 run through the loop.

In fact if you never have a folder to organise your clusters under the datacenter, you could do (which is fact an implicite 1 run through loop)

$cl = Get-View -ViewType ClusterComputeResource -Property Name,Parent

$dc = Get-View -Id (Get-view -Id $cl.Parent -Property Name,Parent).Parent -Property Name

$dc.Name

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
lanwench
Enthusiast
Enthusiast

Brilliant! That is super fast.

We don't have any cluster folders (I'm not altogether certain I knew that was possible) - just VM folders.

So, *this* gives me what I'm looking for ... is there anything even better?

Get-View -ViewType ClusterComputeResource -Property Name,Parent | Foreach-Object {

    New-Object PSObject -Property @{

        Cluster = $_.Name

        Datacenter = &{(Get-View -Id (Get-view -Id $_.Parent -Property Name,Parent).Parent -Property Name).Name}

    }

}

I'm on a roll here. Stop me. Smiley Happy

0 Kudos
LucD
Leadership
Leadership

Not that I can think off :smileycool:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference