VMware Cloud Community
edge10
Contributor
Contributor
Jump to solution

help

Hi Guys,

I am using the following script that lists all the VM in a specific cluster

get-cluster "cluster1", "cluster2" | get-vm | select  name, notes, vmhost

it works really well, but I was wondering how I could also list the cluster name?

Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, edge10-

At least a couple of ways you could go about that.  One would be to access the "Parent" property of the VM's VMHost property, which is the VMHost's cluster:

## use calculated property from VM's VMHost's "parent" property
Get-Cluster "cluster1", "cluster2" | Get-VM | select Name, Notes, VMHost, @{n="ClusterName"; e={$_.VMHost.Parent.Name}}

Or, you could use a Foreach-Object (alias "%") loop to keep/use the string that you pass for the cluster name:

## use a calculated property that is just the variable that holds the current cluster's name
"cluster1", "cluster2" | %{$strClusterName = $_; Get-Cluster $strClusterName | Get-VM} | select Name, Notes, VMHost, @{n="ClusterName"; e={$strClusterName}}

The latter should be a touch faster, but both should get you something like:

Name         Notes     VMHost      ClusterName
----         -----     ------      -------
someVM124    test      myHost0     testCluster
ubuntu02     prod      myHost21    prodCluster
...

How's that?

View solution in original post

Reply
0 Kudos
2 Replies
mattboren
Expert
Expert
Jump to solution

Hello, edge10-

At least a couple of ways you could go about that.  One would be to access the "Parent" property of the VM's VMHost property, which is the VMHost's cluster:

## use calculated property from VM's VMHost's "parent" property
Get-Cluster "cluster1", "cluster2" | Get-VM | select Name, Notes, VMHost, @{n="ClusterName"; e={$_.VMHost.Parent.Name}}

Or, you could use a Foreach-Object (alias "%") loop to keep/use the string that you pass for the cluster name:

## use a calculated property that is just the variable that holds the current cluster's name
"cluster1", "cluster2" | %{$strClusterName = $_; Get-Cluster $strClusterName | Get-VM} | select Name, Notes, VMHost, @{n="ClusterName"; e={$strClusterName}}

The latter should be a touch faster, but both should get you something like:

Name         Notes     VMHost      ClusterName
----         -----     ------      -------
someVM124    test      myHost0     testCluster
ubuntu02     prod      myHost21    prodCluster
...

How's that?

Reply
0 Kudos
edge10
Contributor
Contributor
Jump to solution

that is exactly what I am after

thank you.

Reply
0 Kudos