Highlighted
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!

0 Kudos
4 Replies
Highlighted
User Moderator
User Moderator

Something like this?

Get-VM |

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


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

0 Kudos
Highlighted
Contributor
Contributor

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

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

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Contributor
Contributor

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

0 Kudos