VMware Cloud Community
DonData
Contributor
Contributor
Jump to solution

Search for Required Hosts in Specific Clusters from vCenters with Total Hosts, Guests, CPU and MEM

Hi All,

Looking for a solution to report by filtering hosts only with custom attribute of 'Host.License' and value of 'Microsoft' from a number of vcenters that has a specific cluster name like 'vc-mix-*'.

From this group of filtered hosts in that specific cluster, tabulate a summary of the totol number of hosts found with total CPU and MEM they have and used.

Also tabluate a summary of the total number of guests found in these hosts in that specific cluster, as well as the total CPU and MEM these guests are using.

I am new to coding and will appreciate any help please? Please let me know if you need more info. Thank you.


---------------------------------------------------------------------------------------------------------------

Kudos are welcome and returned! Twitter@DonLowYH

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this

# Connect to all the vCenters
Connect-VIServer -Server 'vc1', 'vc2', 'vc3' -User 'xxx' -Password 'yyy'

foreach ($vc in $global:defaultVIServers) {
  $ca = Get-CustomAttribute -Name 'Host.License' -Server $vc

  Get-Cluster -Name "vc-mix-*" -Server $vc -PipelineVariable cluster |
  ForEach-Object -Process {
    $esx = Get-VMHost -Location $cluster | Where-Object { (Get-Annotation -Entity $_ -CustomAttribute $ca).Value -eq 'Microsoft' }
    $vm = Get-VM -Location $esx
    $stats = Get-Stat -Entity $vm -Stat 'cpu.usagemhz.average', 'mem.consumed.average' -Realtime -MaxSamples 1 -Instance '' -ErrorAction SilentlyContinue

    New-Object -TypeName PSObject -Property ([ordered]@{
        vCenter = $vc.Name
        Cluster = $cluster.Name
        VMHost = $esx.Name -join '|'
        VMHostTotalCpuMhz = ($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum
        VMHostTotalCpuMhzUsed = ($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum
        VMHostTotalMemGb = ($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum
        VMHostTotalMemGbUsed = ($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum
        VMCount = $vm.Count
        VMTotalCpuMhzUsed = ($stats | Where-Object { $_.MetricId -eq 'cpu.usagemhz.average' } | Measure-Object -Property Value -Sum).Sum
        VMTotalMemMbUsed = [math]::Round(( $stats | Where-Object { $_.MetricId -eq 'mem.consumed.average' } | Measure-Object -Property Value -Sum).Sum / 1KB, 1)
      })
  }
}


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this

# Connect to all the vCenters
Connect-VIServer -Server 'vc1', 'vc2', 'vc3' -User 'xxx' -Password 'yyy'

foreach ($vc in $global:defaultVIServers) {
  $ca = Get-CustomAttribute -Name 'Host.License' -Server $vc

  Get-Cluster -Name "vc-mix-*" -Server $vc -PipelineVariable cluster |
  ForEach-Object -Process {
    $esx = Get-VMHost -Location $cluster | Where-Object { (Get-Annotation -Entity $_ -CustomAttribute $ca).Value -eq 'Microsoft' }
    $vm = Get-VM -Location $esx
    $stats = Get-Stat -Entity $vm -Stat 'cpu.usagemhz.average', 'mem.consumed.average' -Realtime -MaxSamples 1 -Instance '' -ErrorAction SilentlyContinue

    New-Object -TypeName PSObject -Property ([ordered]@{
        vCenter = $vc.Name
        Cluster = $cluster.Name
        VMHost = $esx.Name -join '|'
        VMHostTotalCpuMhz = ($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum
        VMHostTotalCpuMhzUsed = ($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum
        VMHostTotalMemGb = ($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum
        VMHostTotalMemGbUsed = ($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum
        VMCount = $vm.Count
        VMTotalCpuMhzUsed = ($stats | Where-Object { $_.MetricId -eq 'cpu.usagemhz.average' } | Measure-Object -Property Value -Sum).Sum
        VMTotalMemMbUsed = [math]::Round(( $stats | Where-Object { $_.MetricId -eq 'mem.consumed.average' } | Measure-Object -Property Value -Sum).Sum / 1KB, 1)
      })
  }
}


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

DonData
Contributor
Contributor
Jump to solution

Wow, if only I could code half like you LucD! 👍


---------------------------------------------------------------------------------------------------------------

Kudos are welcome and returned! Twitter@DonLowYH

Reply
0 Kudos