VMware Cloud Community
gerf0727
Enthusiast
Enthusiast
Jump to solution

How to list VMs within a host, datastore and specified cluster ?

Hello,
I found the folllowing code but not able to figure it out. The ouput is not what I want.

Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -Name "Site1UCSCluster01" }}, @{N="ESX Host";E={Get-VMHost -VM $_}},@{N="Datastore";E={Get-Datastore -VM $_}} |fl

But, the output is wrong those VMs do not reside on Site1UCSCluster01:

Name      : CBEDM
Cluster   : Site1UCSCluster01
ESX Host  : Host1
Datastore : CBEDM-DS3-R5

Name      : CBEDMTest
Cluster   : Site1UCSCluster01
ESX Host  : Host2
Datastore : CBTEST-DS1-R5

thanks for ur help,

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Then try it like this

$clusterName = "MyCluster" 

Get-Cluster
-Name $clusterName | Get-VM |
Select Name,
@{N="Cluster";E={$clusterName}},
@{N="ESX Host";E={Get-VMHost -VM $_ | Select -ExpandProperty Name}},
@{N="Datastore";E={Get-Datastore -VM $_ | Select -ExpandProperty Name}} |
fl


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

That line does not only list VMs on a specific cluster, it lists all VMs and then tries to report on which cluster they are.

But unfortunately it will not work that way, the VM parameter is required instead of the Name parameter

Try it like this

Get-VM | 
Select
Name,
@{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},
@{N="ESX Host";E={Get-VMHost -VM $_ | Select -ExpandProperty Name}},
@{N="Datastore";E={Get-Datastore -VM $_ | Select -ExpandProperty Name}} |
fl


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

gerf0727
Enthusiast
Enthusiast
Jump to solution

Hmm, now it is listing all the clusters, what if I want a specified cluster...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then try it like this

$clusterName = "MyCluster" 

Get-Cluster
-Name $clusterName | Get-VM |
Select Name,
@{N="Cluster";E={$clusterName}},
@{N="ESX Host";E={Get-VMHost -VM $_ | Select -ExpandProperty Name}},
@{N="Datastore";E={Get-Datastore -VM $_ | Select -ExpandProperty Name}} |
fl


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

0 Kudos