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)
Hostname | No.of total VM's | Power-on VM's | Power-off VM | CPU Utilization | Memory Utilization |
ABC | |||||
XYZ | |||||
ABC | |||||
XYZ |
Change those 2 lines to
MemUtilPerc = [math]::Round(($report | Measure-Object -Property MemUtilPerc -Average).Average,2)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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".
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
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
Try replacnig that line with
Body = $report | ConvertTo-Html | Out-String
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Try like this
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
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
Change those 2 lines to
MemUtilPerc = [math]::Round(($report | Measure-Object -Property MemUtilPerc -Average).Average,2)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect, thank you !!!
Hi Luc,
I notice the poweroff/poweron numbers are not valid - it seems it's duplicating the values
VMPoweredOn | VMPoweredOff | CpuUtilPerc | MemUtilPerc |
17 | 17 |
| |
18 | 18 |
| |
18 | 18 |
| |
19 | 19 |
| |
18 | 18 |
| |
18 | 18 |
| |
18 | 18 |
| |
18 | 18 |
| |
18 | 18 |
| |
18 | 18 |
| |
18 | 18 |
| |
18 | 18 |
| |
18 | 18 |
| |
234 | 234 |
|
Find this part
VMPoweredOn = ($vms | where{$_.PowerState -eq 'PoweredOn'}).Count
VMPoweredOff = ($vms | where{$_.PowerState -eq 'PoweredOn'}).Count
And change the second one to PoweredOff
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