The idea behind this PowerCLI script is to generate an HTML page and retrieve stats from an entity (a virtual machine) by using the Get-Stat cmdlet. These stats will be passed as data upon which charts will be created.
The PowerCLI cmdlet Get-Stat retrieve stats from any powered on virtual machine:
Get-Stat -Entity (Get-VMHost -Name 10.0.1.62 | Get-VM | Where-Object PowerState -match "PoweredOn") -Stat $stat -Start (Get-Date).AddHours(-24) -MaxSamples (10) -IntervalMins 10 | Measure-Object Value -Average
-Entity: the object against you want to retrieve stats from. This could be a datacenter, a virtual machine, a host, etc. In the line above it retrieves all powered on VMs running on host 10.0.1.62.
-Stat: the statistic you want to retrieve.
The following values are accepted:
-Start: start time of statistics retrieval
-MaxSamples: how many samples consider for the measurement
-IntervalMins: the amount of time that separates two consecutive measurements
-Measure-Object Value: we need to specify we are going to use this value as a measure.
-Average: how this measure will be considered. Average, Maximum and Minimum are accepted values. Average consider the average resulting value from all collected samples. Please note that for every of the available stats the Average value is a good indicator since the average usage of CPU, Memory, Disk or Network over an amount of time could be considered as a good indicator of how well the measured entity performs. sys.uptime.latest is the only measure for that average is not suited, since we need to retrieve only the last value of uptime in this case Maximum is the correct way to measure it.
The output produced by the PowerCLI script is an HTML file and this means that you can style it applying a proper CSS.
This is an example of a chart report I generated:
The following is the sample PowerCLI code used to generate a donut chart, as usual you can find it also on my GitHub repository: Stats using Google charts.ps1
As you can see this time HTML page is generated by simply concatenating variables $htmlheader, $data and $htmlfooter containing the HTML code. This is another, less elegant way though, to generate the HTML page. Conversely you can use the ConvertTo-Html cmdlet as in fancy reports using PowerCLI.