VMware Cloud Community
electricd7
Contributor
Contributor

Return the cluster name for a given VM name?

I am working on a script in which I need to get the current clustername for a given VM.  Is there a one-liner type function that would perform the following idea:

$clustername = Get-Cluster($VMname)

Obviously that function isn't valid, but that is what I am trying to accomplish.  Thanks!

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

Something like this?

Get-VM |

Select Name,@{N='Cluster';E={(Get-Cluster -VM $_).Name}}


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

Reply
0 Kudos
Dharzhak
Enthusiast
Enthusiast

If you're not wanting to display a calculated property in that manner, this will work:

$clustername = (Get-VM -Name $VMname | Get-Cluster).Name

Reply
0 Kudos
LucD
Leadership
Leadership

If you don't want to use 2 cmdlets, you could do

$clustername = Get-Cluster -VM $VMname | Select -ExpandProperty Name


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

Reply
0 Kudos
Dharzhak
Enthusiast
Enthusiast

Select-Object is a cmdlet. 😉

If efficiency is a concern (probably not in this case), you can remove the unnecessary pipe to Select-Object:

$clustername = (Get-Cluster -VM $VMname).Name

Reply
0 Kudos