VMware Cloud Community
VijayKumarMyada
Enthusiast
Enthusiast

Cluster usage along with hosts and vm count

Hello!

am trying to modifying below script to give me the number of hosts and vms in the cluster along with the usage metrics.

Connect-VIServer -Server XXX -User domain\xx -Password xxx

$report = @()

$Date=Get-Date

$clusterName = "*"

foreach($cluster in Get-Cluster -Name $clusterName){

    $esx = $cluster | Get-VMHost   

    $ds = Get-Datastore -VMHost $esx | where {$_.Type -eq "VMFS" -and $_.Extensiondata.Summary.MultipleHostAccess}

    $vms=Get-Cluster | Select @{N="VM Count"; E={($_ | Get-VM).Count}}

    $hosts=Get-Cluster | Select Name, @{N="Host Count"; E={($_ | Get-VMHost).Count}}

    $statcpu = Get-Stat -Entity ($esx)-start 03/22/2020 -Finish 04/04/2020 -MaxSamples 10000 -stat cpu.usage.average

    $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum

     

      $statmem = Get-Stat -Entity ($esx)-start 03/22/2020 -Finish 04/04/2020 -MaxSamples 10000 -stat mem.usage.average

      $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum

       

    $row = "" | Select VCname,DCname,Clustername,"Total Physical Memory (GB)",

                "Configured Memory GB","Available Memroy (GB)",

                "Total CPU (Mhz)","Configured CPU (Mhz)",

                "Available CPU (Mhz)","Total Disk Space (GB)",

                "Configured Disk Space (GB)","Available Disk Space (GB)","Total Disk Space (TB)",

                "Configured Disk Space (TB)","Available Disk Space (TB)","CPUCount","CPUAverage","CPUMax","CPUMin","MemoryAverage","MemoryMax","MemoryMin","VMCount"

    $row.VCname = $cluster.Uid.Split(':@')[1]

    $row.DCname = (Get-Datacenter -Cluster $cluster).Name

    $row.Clustername = $cluster.Name

    $row."Total Physical Memory (GB)" = ($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum

    $row."Configured Memory GB" = ($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum

    $row."Available Memroy (GB)" = ($esx | Measure-Object -InputObject {$_.MemoryTotalGB - $_.MemoryUsageGB} -Sum).Sum

    $row."Total CPU (Mhz)" = ($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum

    $row."Configured CPU (Mhz)" = ($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum

    $row."Available CPU (Mhz)" = ($esx | Measure-Object -InputObject {$_.CpuTotalMhz - $_.CpuUsageMhz} -Sum).Sum

$row."Total Disk Space (GB)" = ($ds | where {$_.Type -eq "VMFS"} | Measure-Object -Property CapacityGB -Sum).Sum

    $row."Configured Disk Space (GB)" = ($ds | Measure-Object -InputObject {$_.CapacityGB - $_.FreeSpaceGB} -Sum).Sum

    $row."Available Disk Space (GB)" = ($ds | Measure-Object -Property FreeSpaceGB -Sum).Sum

    $row."Total Disk Space (TB)" = ((($ds | where {$_.Type -eq "VMFS"} | Measure-Object -Property CapacityGB -Sum).Sum)/ (1024))

    $row."Configured Disk Space (TB)" = ((($ds | Measure-Object -InputObject {$_.CapacityGB - $_.FreeSpaceGB} -Sum).Sum)/ (1024))

    $row."Available Disk Space (TB)" = ((($ds | Measure-Object -Property FreeSpaceGB -Sum).Sum) / (1024))

    $row."CPUCount" = $cpu.count

    $row."CPUAverage" = $cpu.Average

    $row."CPUMax" = $cpu.Maximum

    $row."CPUMin" = $cpu.Minimum

    $row."MemoryAverage" = $mem.Average

    $row."MemoryMax" = $mem.Maximum

    $row."MemoryMin" = $mem.Minimum

    $row.VMCount = $vms

   $row.hostcount=$hosts

    $report += $row

}

$report | Export-Csv "C:\temp\xx.csv" -NoTypeInformation -UseCulture

below is the output, vmcount and host count showing as system.object.

pastedImage_8.png

Tags (1)
0 Kudos
2 Replies
jburen
Expert
Expert

When you create $hosts you end up with an object of type VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl. If you look at the content of that object you have two properties: Name and Host Count.

First I would remove the space from Host Count and use HostCount. Then you can use this: $row.hostcount=$hosts.HostCount

This should also work for $vms

Consider giving Kudos if you think my response helped you in any way.
0 Kudos
LucD
Leadership
Leadership

Try like this

Connect-VIServer -Server XXX -User domain\xx -Password xxx

$report = @()

$Date = Get-Date

$clusterName = "*"

foreach ($cluster in Get-Cluster -Name $clusterName) {

    $esx = $cluster | Get-VMHost 

    $ds = Get-Datastore -VMHost $esx | Where-Object { $_.Type -eq "VMFS" -and $_.Extensiondata.Summary.MultipleHostAccess }

    $statcpu = Get-Stat -Entity ($esx)-start 03/22/2020 -Finish 04/04/2020 -MaxSamples 10000 -stat cpu.usage.average

    $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum

   

    $statmem = Get-Stat -Entity ($esx)-start 03/22/2020 -Finish 04/04/2020 -MaxSamples 10000 -stat mem.usage.average

    $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum

     

    $row = "" | Select-Object VCname, DCname, Clustername, "Total Physical Memory (GB)",

    "Configured Memory GB", "Available Memroy (GB)",

    "Total CPU (Mhz)", "Configured CPU (Mhz)",

    "Available CPU (Mhz)", "Total Disk Space (GB)",

    "Configured Disk Space (GB)", "Available Disk Space (GB)", "Total Disk Space (TB)",

    "Configured Disk Space (TB)", "Available Disk Space (TB)", "CPUCount", "CPUAverage", "CPUMax", "CPUMin", "MemoryAverage", "MemoryMax", "MemoryMin",

    "HostCount", "VMCount"

    $row.VCname = $cluster.Uid.Split(':@')[1]

    $row.DCname = (Get-Datacenter -Cluster $cluster).Name

    $row.Clustername = $cluster.Name

    $row."Total Physical Memory (GB)" = ($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum

    $row."Configured Memory GB" = ($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum

    $row."Available Memroy (GB)" = ($esx | Measure-Object -InputObject { $_.MemoryTotalGB - $_.MemoryUsageGB } -Sum).Sum

    $row."Total CPU (Mhz)" = ($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum

    $row."Configured CPU (Mhz)" = ($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum

    $row."Available CPU (Mhz)" = ($esx | Measure-Object -InputObject { $_.CpuTotalMhz - $_.CpuUsageMhz } -Sum).Sum

    $row."Total Disk Space (GB)" = ($ds | Where-Object { $_.Type -eq "VMFS" } | Measure-Object -Property CapacityGB -Sum).Sum

    $row."Configured Disk Space (GB)" = ($ds | Measure-Object -InputObject { $_.CapacityGB - $_.FreeSpaceGB } -Sum).Sum

    $row."Available Disk Space (GB)" = ($ds | Measure-Object -Property FreeSpaceGB -Sum).Sum

    $row."Total Disk Space (TB)" = ((($ds | Where-Object { $_.Type -eq "VMFS" } | Measure-Object -Property CapacityGB -Sum).Sum) / (1024))

    $row."Configured Disk Space (TB)" = ((($ds | Measure-Object -InputObject { $_.CapacityGB - $_.FreeSpaceGB } -Sum).Sum) / (1024))

    $row."Available Disk Space (TB)" = ((($ds | Measure-Object -Property FreeSpaceGB -Sum).Sum) / (1024))

    $row."CPUCount" = $cpu.count

    $row."CPUAverage" = $cpu.Average

    $row."CPUMax" = $cpu.Maximum

    $row."CPUMin" = $cpu.Minimum

    $row."MemoryAverage" = $mem.Average

    $row."MemoryMax" = $mem.Maximum

    $row."MemoryMin" = $mem.Minimum

    $row.VMCount = ($esx.extensiondata.vm.Count | Measure-Object -Sum).Sum

    $row.hostcount = $cluster.ExtensionData.Host.Count

    $report += $row

}

$report | Export-Csv "C:\temp\xx.csv" -NoTypeInformation -UseCulture


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

0 Kudos