I was having an issue with Get-Stat pulling any single value. This command was run against 800 Esxi hosts and most took only seconds to respond on many hosts; However, I was seeing some take as long as 2 minutes....
Get-Stat -Entity ($vmHost) -start (get-date).AddDays(-1) -Finish (Get-Date) -IntervalMins 5 -stat cpu.usage.average
After some investigation, I found the hosts that take minutes are returning multiple 'instances' of the same value and instead of returning a few hundred records, would return 12,000 records, even for past day 5 min intervals.
Thinking I could add the -MaxSamples to limit the result, I changed the command to this....
Get-Stat -Entity ($vmHost) -start (get-date).AddDays(-1) -Finish (Get-Date) -IntervalMins 5 -MaxSamples 300 -stat cpu.usage.average
or even this....
Get-Stat -Entity ($vmHost) -start (get-date).AddDays(-1) -Finish (Get-Date) -IntervalMins 5 -MaxSamples 8 -stat cpu.usage.average
However, I still get thousands of records back and many minutes to respond on certain ones.
On the fast hosts, the MaxSamples works and limits the number of records, but on the hosts that are slow, the MaxSamples seems to be ignored and still provides multiple "instances" of the object. I have tried -Instance "" and again, the ones that are fast will respect the filter, but the ones that are slow do not.
This is a sample what I get regardless of the settings....
MetricId Timestamp Value Unit Instance
-------- --------- ----- ---- --------
cpu.usage.average 3/23/2020 2:05:00 PM 0.32 % 5
cpu.usage.average 3/23/2020 2:00:00 PM 0.15 % 5
cpu.usage.average 3/23/2020 1:55:00 PM 0.31 % 5
cpu.usage.average 3/23/2020 1:50:00 PM 0.54 % 5
cpu.usage.average 3/23/2020 1:45:00 PM 0.06 % 5
cpu.usage.average 3/23/2020 2:20:00 PM 0.12 % 6
cpu.usage.average 3/23/2020 2:15:00 PM 0.35 % 6
cpu.usage.average 3/23/2020 2:10:00 PM 0.49 % 6
cpu.usage.average 3/23/2020 2:05:00 PM 0.36 % 6
cpu.usage.average 3/23/2020 2:00:00 PM 0.6 % 6
cpu.usage.average 3/23/2020 1:55:00 PM 0.26 % 6
cpu.usage.average 3/23/2020 1:50:00 PM 0.29 % 6
cpu.usage.average 3/23/2020 1:45:00 PM 0.75 % 6
cpu.usage.average 3/23/2020 2:20:00 PM 0.21 % 7
cpu.usage.average 3/23/2020 2:15:00 PM 0.42 % 7
cpu.usage.average 3/23/2020 2:10:00 PM 0.12 % 7
cpu.usage.average 3/23/2020 2:05:00 PM 0.11 % 7
cpu.usage.average 3/23/2020 2:00:00 PM 0.28 % 7
cpu.usage.average 3/23/2020 1:55:00 PM 0.11 % 7
cpu.usage.average 3/23/2020 1:50:00 PM 0.15 % 7
cpu.usage.average 3/23/2020 1:45:00 PM 0.33 % 7
cpu.usage.average 3/23/2020 2:20:00 PM 0.23 % 8
cpu.usage.average 3/23/2020 2:15:00 PM 0.33 % 8
cpu.usage.average 3/23/2020 2:10:00 PM 0.18 % 8
cpu.usage.average 3/23/2020 2:05:00 PM 0.2 % 8
cpu.usage.average 3/23/2020 2:00:00 PM 0.06 % 8
cpu.usage.average 3/23/2020 1:55:00 PM 0.06 % 8
cpu.usage.average 3/23/2020 1:50:00 PM 0.09 % 8
cpu.usage.average 3/23/2020 1:45:00 PM 1.18 % 8
cpu.usage.average 3/23/2020 2:20:00 PM 0.11 % 9
Depending on the Historical Interval you select, the Get-Stat cmdlet for the cpu.usage.average metric will return a value per core and an aggregate value.
If you don't want the core values, use the Instance parameter to specify you only want the aggregate value (with an empty string).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
As mentioned, i tried -Instance "", but you show only one ". When I do only one ", I get the error as it appears as an unfinished quote.....
to be clear... -Instance "" does filter out the extra instances, but it still takes 2 minutes.
That is not a double quote, but 2 single quotes (an empty string)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Seems the filtering works, but does not cut down the query. Going to assume here that the query is run against the vcenter's database. These are 6.5 vcsa vcenters, but maybe there is a difference that is affecting the speed.
PowerCLI C:\> Measure-Command {$testval0 = Get-Stat -Entity ($vmHost) -start (get-date).AddDays(-1) -Finish (Get-Date) -IntervalMins 5 -MaxSamples 8 -stat cpu.usage.average -Instance "" }
TotalMinutes : 2.35271660333333
PowerCLI C:\> Measure-Command {$testval1 = Get-Stat -Entity ($vmHost) -start (get-date).AddDays(-1) -Finish (Get-Date) -IntervalMins 5 -MaxSamples 300 -stat cpu.usage.average -Instance "" }
TotalMinutes : 2.34281446
PowerCLI C:\> Measure-Command {$testval2 = Get-Stat -Entity ($vmHost) -start (get-date).AddDays(-1) -Finish (Get-Date) -IntervalMins 5 -stat cpu.usage.average }
TotalMinutes : 2.33756025666667
PowerCLI C:\> $testval0.count
8
PowerCLI C:\> $testval1.count
285
PowerCLI C:\> $testval2.count
11685
If your query is not for Realtime (+/- the last hour) it is indeed against the vCenter DB.
Try leaving out the -IntervalMins parameter, that is, in any case, the default for metrics that start more than 1 hour ago and less than 24 hours.
Finish is by default the current datetime.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference