CPU and MEM usage on regular intervals

This topic contains 2 replies, has 2 voices, and was last updated by  Powershell 2 years, 1 month ago.

  • Author
  • #28824


    i want to know the CPU and MEM usage of my server every 5 minutes and append that in a single html, but i am unable to do that. HTML creates headers every time the script runs, any suggestions? how can we append data to html file

  • #28829

    Rob Simmers

    You're not saying how you are trying to accomplish this. If you want to dynamically update an HTML page, you need to look at InternetExplorer.Application. Cobbling code together from here and here, I came up with a basic example that will get memory statistics every 10 seconds and update the html:

    function Get-MemoryStats {
        param (
        $fieldfreeram = @{label = "Available Physical Memory (MB)"; Expression = {[math]::round(($_.FreePhysicalMemory / 1kb), 2)}};  
        $fieldtotalram = @{label = "Total Physical Memory (MB)"; Expression = {[math]::round(($_.TotalVisibleMemorySize / 1kb), 2)}};  
        $fieldfreeVram = @{label = "Available Virtual Memory (MB)"; Expression = {[math]::round(($_.FreeVirtualMemory / 1kb), 2)}};  
        $fieldtotalVram = @{label = "Total Virtual Memory (MB)"; Expression = {[math]::round(($_.TotalVirtualMemorySize /1kb), 2)}};  
        $memtotal = Get-WmiObject -Class win32_OperatingSystem -ComputerName localhost | Select $fieldfreeram,$fieldtotalram,$fieldfreeVram,$fieldtotalVram  
        if ($AsHTMLTblFrag) {
            #You can't really use HTML fragment because it generates an entire table
            #and we just need the row html, so if the switch is passed then wrap
            #the content with TD tags
            $memtotal | foreach{"{0}{1}{2}{3}" -f $_."Available Physical Memory (MB)", 
                                                                                      $_."Total Physical Memory (MB)", 
                                                                                      $_."Available Virtual Memory (MB)", 
                                                                                      $_."Total Virtual Memory (MB)"}
        else {
    $oIE=new-object -com internetexplorer.application
    #Create a blank webpage
    while ($oIE.busy) {
        sleep -milliseconds 50
    $oDocBody=$oIE.document.documentelement.lastchild ;
    #populate the document.body
    $oDocBody.innerhtml=$(Get-MemoryStats | ConvertTo-HTML)
    $oDocBody.style.font="12pt Arial";
    #Prepare a title.
    $oTitle.style.font="bold 20pt Arial"
    $oTitle.innerhtml="Memory Stats";
    $oTitle.align="center" ;
    #Display the title before the Table object.
    $oTable=@($oIE.document.getElementsByTagName("TABLE"))[0] ;
    $oDocBody.insertBefore($oTitle,$oTable) > $null;
    #Reading back from IE.
    $oTBody=@($oIE.document.getElementsByTagName("TBODY"))[0] ;
    for ($i=0;$i -le 5;$i++) {
        Sleep 10
        #Insert a row into the existing table created with the 
        #original ConvertTo-HTML of Get-MemoryStats
        $row = $oTbody.InsertRow(1)
        $row.InnerHTML = $(Get-MemoryStats -AsHTMLTblFrag)
  • #28853


    Sorry, i am doing it like this
    [array]$results += [PSCustomObject] @{
    Time = "$($time.'Time')"
    'CPU-Usage' = "$($CPUAvg.Average)%"
    'Mem-Usage' = "$($MemAvg.MemoryUsage)%"
    $results | ConvertTo-Html -head $head –body $body | add-content c:\server.html

You must be logged in to reply to this topic.