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!
Something like this?
Select Name,@{N='Cluster';E={(Get-Cluster -VM $_).Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If you're not wanting to display a calculated property in that manner, this will work:
$clustername = (Get-VM -Name $VMname | Get-Cluster).Name
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
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