Hello,
I have this script where I can get some info from each VM,
I would like to add:
CPU assigned/used
Memory assigned/used
Get-VM |select Guest,NumCpu,MemoryMB,Host,UsedSpaceGB,ProvisionedSpaceGB,Name|Export-Csv -path “C:\Users\gemela\Hardware_info.csv” -NoTypeInformation
This can be done in multiple ways. In fact that ration is the "usage" percentage for memory and CPU.
I used two rather simple methods.
Get-VM |
select Guest,NumCpu,MemoryMB,Host,UsedSpaceGB,ProvisionedSpaceGB,Name,
@{N='CPU ratio';E={"$(Get-Stat -Entity $_ -Stat cpu.usage.average -Realtime -MaxSamples 1 | select -ExpandProperty Value) %"}},
@{N='Memory ratio';E={"{0:P}" -f ($_.ExtensionData.Summary.Quickstats.GuestMemoryUsage/$_.MemoryMB)}} |
Export-Csv -path “C:\Users\gemela\Hardware_info.csv” -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
This will give me like 'real time' of use? I mean percentage of use when script is in execution?
Correct, the Realtime switch on the Get-Stat cmdlet returns realtime statistics.
The QuickStats properties are "updated with near real-time regularity."
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It would be possible to set some time like weekly, monthly or also some hours?
Sure, then we will have to use the Get-Stat cmdlet exclusively.
Something like this for the ratios over the last 7 days for example.
Get-VM |
select Guest,NumCpu,MemoryMB,Host,UsedSpaceGB,ProvisionedSpaceGB,Name,
@{N='CPU ratio';E={
$finish = Get-Date
$start = $finish.AddDays(-1)
$script:stats = Get-Stat -Entity $_ -Stat 'cpu.usage.average','mem.usage.average' -Start $start -Finish $finish
$script:stats | where{$_.MetricId -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average |
Select -ExpandProperty Average}},
@{N='Memory ratio';E={
$script:stats | where{$_.MetricId -eq 'mem.usage.average'} | Measure-Object -Property Value -Average |
Select -ExpandProperty Average
}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello, this is a good suggestion!
I was wondering - is it possible to sort the results (CPU and Memory ratio)? New to powershell and dont seem to get the results sorting.
You can pass the results over the pipeline to Sort-Object.
@JanisKuziks wrote:
Hello, this is a good suggestion!
I was wondering - is it possible to sort the results (CPU and Memory ratio)? New to powershell and dont seem to get the results sorting.
Get-VM |
select Guest,NumCpu,MemoryMB,Host,UsedSpaceGB,ProvisionedSpaceGB,Name,
@{N='CPU ratio';E={
$finish = Get-Date
$start = $finish.AddDays(-1)
$script:stats = Get-Stat -Entity $_ -Stat 'cpu.usage.average','mem.usage.average' -Start $start -Finish $finish
$script:stats | where{$_.MetricId -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average |
Select -ExpandProperty Average}},
@{N='Memory ratio';E={
$script:stats | where{$_.MetricId -eq 'mem.usage.average'} | Measure-Object -Property Value -Average |
Select -ExpandProperty Average
}} |
Sort-Object -Property 'CPU ratio','Memory ratio' -Descending
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Wow, that was very fast LucD!
Thank you very much, this will come very handy👍