VMware Cloud Community
DevD
Enthusiast
Enthusiast
Jump to solution

vcenter Cluster report

I am looking for a script to run against the vcenter to generate the following table  send it in email body  (if not possible in body than as attached csv)

                                                                                                                                                                                
HostnameNo.of total VM'sPower-on VM'sPower-off VMCPU UtilizationMemory Utilization
ABC     
XYZ     
ABC     
XYZ     
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Change those 2 lines to

   CpuUtilPerc = [math]::Round(($report | Measure-Object -Property CpuUtilPerc -Average).Average,2)

   MemUtilPerc = [math]::Round(($report | Measure-Object -Property MemUtilPerc -Average).Average,2)


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

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

div>$report = Get-Cluster -PipelineVariable cluster |

Get-VMHost -PipelineVariable esx |

ForEach-Object -Process {

   $vms = $esx | Get-VM

   New-Object PSObject -Property ([ordered]@{

   Cluster = $cluster.Name

   VMHost = $esx.Name

   VMPoweredOn = ($vms | where{$_.PowerState -eq 'PoweredOn'}).Count

   VMPoweredOffn = ($vms | where{$_.PowerState -eq 'PoweredOn'}).Count

   CpuUtilPerc = (Get-stat -Entity $esx -Stat cpu.usage.average -Realtime -MaxSamples 1 -Instance '').Value

   MemUtilPerc = (Get-stat -Entity $esx -Stat mem.usage.average -Realtime -MaxSamples 1).Value

   })

}

$sMail = @{

   From = 'me@domain'

   To = 'you@domsain'

   Subject = 'Cluster report'

   Body = $report | ConvertTo-Html

   SmtpServer = smtp@domain

   BodyAsHtml = $true

}

Send-MailMessage @sMail


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

0 Kudos
DevD
Enthusiast
Enthusiast
Jump to solution

Hi Luc, Thanks for responding.

Script failed with following error

Get-stat : 4/8/2019 9:11:06 AM    Get-Stat        The metric counter "cpu.usage.average" doesn't exist for entity "XXXX.XXX.com".   

At D:\Scripts\Clusterreport.ps1:19 char:19

+ ... UtilPerc = (Get-stat -Entity $esx -Stat cpu.usage.average -Realtime - ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ResourceUnavailable: (cpu.usage.average:String) [Get-Stat], VimException

    + FullyQualifiedErrorId : Client20_RuntimeDataServiceImpl_CheckUserMetrics_MetricDoesntExist,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetViStats

Get-stat : 4/8/2019 9:11:06 AM    Get-Stat        Stat with instance '' was not found using the specified filter(s).   

At D:\Anoop\Scripts\Clusterreport.ps1:19 char:19

+ ... UtilPerc = (Get-stat -Entity $esx -Stat cpu.usage.average -Realtime - ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Get-Stat], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetViStats

Get-stat : 4/8/2019 9:11:06 AM    Get-Stat        The metric counter "mem.usage.average" doesn't exist for entity "XXXX.XXXX.com".   

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you are not collecting statistical data on that ESXi node.
Which is rather bizar.

Do you see performance data for that ESXi node under the Performance tab?


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

0 Kudos
DevD
Enthusiast
Enthusiast
Jump to solution

Yeah you were right now no more than error message but seeing the below one :

Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.

At D:\Scripts\Clusterreport.ps1:49 char:18

+ Send-MailMessage @sMail

+                  ~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try replacnig that line with

Body = $report | ConvertTo-Html | Out-String


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

0 Kudos
DevD
Enthusiast
Enthusiast
Jump to solution

It worked ...Thanks!

is it possible to get the total at the end - especially for no of poweron/power-off VM's.

Cluster VMHost VMPoweredOn VMPoweredOffn CpuUtilPerc MemUtilPerc

HyperFlex Tes1 4 4 1.01 17.72

HyperFlex Test2 2 2 0.63 16.93

HyperFlex Test3 1 1 3.11 15.36

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$report = Get-Cluster -PipelineVariable cluster |

Get-VMHost -PipelineVariable esx |

ForEach-Object -Process {

   $vms = $esx | Get-VM

   New-Object PSObject -Property ([ordered]@{

   Cluster = $cluster.Name

   VMHost = $esx.Name

   VMPoweredOn = ($vms | where{$_.PowerState -eq 'PoweredOn'}).Count

   VMPoweredOff = ($vms | where{$_.PowerState -ne 'PoweredOn'}).Count

   CpuUtilPerc = (Get-stat -Entity $esx -Stat cpu.usage.average -Realtime -MaxSamples 1 -Instance '').Value

   MemUtilPerc = (Get-stat -Entity $esx -Stat mem.usage.average -Realtime -MaxSamples 1).Value

   })

}


$report += New-Object PSObject -Property ([ordered]@{

   Cluster = ''

   VMHost = ''

   VMPoweredOn = ($report | Measure-Object -Property VMPoweredOn -Sum).Sum

   VMPoweredOff = ($report | Measure-Object -Property VMPoweredOff -Sum).Sum

   CpuUtilPerc = ($report | Measure-Object -Property CpuUtilPerc -Average).Average

   MemUtilPerc = ($report | Measure-Object -Property MemUtilPerc -Average).Average

   })


$sMail = @{

   From = 'me@domain'

   To = 'you@domsain'

   Subject = 'Cluster report'

   Body = $report | ConvertTo-Html | Out-String

   SmtpServer = smtp@domain

   BodyAsHtml = $true

}

Send-MailMessage @sMail


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

0 Kudos
DevD
Enthusiast
Enthusiast
Jump to solution

It worked, need to find limit the average output to two dight after "." in sum.

Cluster VMHost VMPoweredOn VMPoweredOff CpuUtilPerc MemUtilPerc

HyperFlex Test1 4 4 1.23 17.72

HyperFlex Test2 2 1.03 16.93

HyperFlex Test3 1 3.38 15.36

                           7 7 1.88000003496806 16.6699997584025

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Change those 2 lines to

   CpuUtilPerc = [math]::Round(($report | Measure-Object -Property CpuUtilPerc -Average).Average,2)

   MemUtilPerc = [math]::Round(($report | Measure-Object -Property MemUtilPerc -Average).Average,2)


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

0 Kudos
DevD
Enthusiast
Enthusiast
Jump to solution

Perfect, thank you !!!

0 Kudos
DevD
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I notice the poweroff/poweron numbers are not valid - it seems it's duplicating the values

VMPoweredOn

VMPoweredOff

CpuUtilPerc

MemUtilPerc

17

17

  1. 0.01
  2. 0.01

18

18

  1. 21.46
  2. 25.81

18

18

  1. 25.15
  2. 24.81

19

19

  1. 21.71
  2. 25.33

18

18

  1. 22.91
  2. 26.92

18

18

  1. 25.78
  2. 24.81

18

18

  1. 26.24
  2. 24.83

18

18

  1. 20.41
  2. 25.84

18

18

  1. 26.74
  2. 25.85

18

18

  1. 29.5
  2. 24.82

18

18

  1. 16.95
  2. 25.89

18

18

  1. 15.46
  2. 24.84

18

18

  1. 13.94
  2. 25.86

234

234

  1. 20.48
  2. 23.51
0 Kudos
sjesse
Leadership
Leadership
Jump to solution

Find this part

VMPoweredOn = ($vms | where{$_.PowerState -eq 'PoweredOn'}).Count

   VMPoweredOff = ($vms | where{$_.PowerState -eq 'PoweredOn'}).Count

And change the second one to PoweredOff

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was indeed an error in the count for the powered off VMs (I put -eq instead of -ne).
I corrected the code above.

Changing it to PoweredOff will not count the Suspended VMs, that is why I used the -ne


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

0 Kudos