5 Replies Latest reply on Aug 21, 2017 3:22 AM by LucD

    Percentage memory and cpu used

    antoniogemelli Novice

      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

        • 1. Re: Percentage memory and cpu used
          LucD Guru
          Community WarriorsvExpertUser Moderators

          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

           

          • 2. Re: Percentage memory and cpu used
            antoniogemelli Novice

            Hi LucD,

            This will give me like 'real time' of use? I mean percentage of use when script is in execution?

            • 3. Re: Percentage memory and cpu used
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              Correct, the Realtime switch on the Get-Stat cmdlet returns realtime statistics.

              The QuickStats properties are "updated with near real-time regularity."

              • 4. Re: Percentage memory and cpu used
                antoniogemelli Novice

                It would be possible to set some time like weekly, monthly  or also some hours?

                • 5. Re: Percentage memory and cpu used
                  LucD Guru
                  vExpertUser ModeratorsCommunity Warriors

                  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

                      }}

                  1 person found this helpful