faherne_CTI
Enthusiast
Enthusiast

Query Multiple vCenters for (vCenter name, Datacenter name, Cluster Name, No. of Cluster Hosts, No. of VMs)

Jump to solution

Hi,

I'm looking to query a list of vCenters (pulled from a text file) for the following information, and then output to a csv file.

I have tried modifying LucD's script documented in the below article, but with no luck.

Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file. unknown

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Try like this

Foreach($vc in $global:DefaultVIServers){

    foreach($dc in Get-Datacenter -Server $vc){

        Get-Cluster -Location $dc -Server $vc |

        Select @{N='VC';E={$vc.Name}},

            @{N='Datacenter';E={$dc.Name}},

            @{N='Cluster';E={$_.Name}},

            @{N='#ESXi';E={$_.ExtensionData.Host.Count}},

            @{N='#VM';E={(Get-View -Id $_.ExtensionData.Host).Vm.Count}}

    }

}


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

View solution in original post

6 Replies
faherne_CTI
Enthusiast
Enthusiast

The table column headers are:

| vCenter Name | Datacenter Name | Cluster Name | No. of Cluster Hosts | No. of Cluster VMs |

0 Kudos
LucD
Leadership
Leadership

Try like this

Foreach($vc in $global:DefaultVIServers){

    foreach($dc in Get-Datacenter -Server $vc){

        Get-Cluster -Location $dc -Server $vc |

        Select @{N='VC';E={$vc.Name}},

            @{N='Datacenter';E={$dc.Name}},

            @{N='Cluster';E={$_.Name}},

            @{N='#ESXi';E={$_.ExtensionData.Host.Count}},

            @{N='#VM';E={(Get-View -Id $_.ExtensionData.Host).Vm.Count}}

    }

}


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

faherne_CTI
Enthusiast
Enthusiast

Thanks Luc, it works a treat!

I added a few mods (that Luc has previously shown me in previous threads) that perhaps others may be interested in:

  • Read in a text file list of vCenters
  • Output table to a CSV file

Connect-viserver -Server (Get-Content C:\Scripts\vCList_test.txt) > $null

$report = Foreach($vc in $global:DefaultVIServers){

    foreach($dc in Get-Datacenter -Server $vc){

        Get-Cluster -Location $dc -Server $vc |

        Select @{N='VC';E={$vc.Name}},

            @{N='Datacenter';E={$dc.Name}},

            @{N='Cluster';E={$_.Name}},

            @{N='#ESXi';E={$_.ExtensionData.Host.Count}},

            @{N='#VM';E={(Get-View -Id $_.ExtensionData.Host).Vm.Count}}

    }

}

$report | Export-Csv C:\Scripts\vClusterListInfo.csv

0 Kudos
PandzicM
Contributor
Contributor

how can we add expresion that will add virtual hardware versions to these VMs also?

Thanks

0 Kudos
LucD
Leadership
Leadership

Not sure I understand the question, the script will have an entry per cluster, not per VM.


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

0 Kudos
PandzicM
Contributor
Contributor

Hi LucD

thanks for reply

what I was looking for is script that will output virtual machines name, virtual hardware version, host name,cluster name and  vcenter where vms running in csv format

sorry for not properly formulated question

Thanks

0 Kudos