CPU and MEM usage on regular intervals

Welcome Forums General PowerShell Q&A CPU and MEM usage on regular intervals

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 years, 4 months ago.

  • Author
    Posts
  • #28824

    Participant
    Points: 0
    Rank: Member

    Hi.
    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

    Participant
    Points: 350
    Helping Hand
    Rank: Contributor

    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 (
            [switch]$AsHTMLTblFrag
        )
        $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 {
            $memtotal
        }
    }
    
    $oIE=new-object -com internetexplorer.application
    #Create a blank webpage
    $oIE.navigate2("About:blank")
    while ($oIE.busy) {
        sleep -milliseconds 50
    }
    $oIE.visible=$true
    
    $oDocBody=$oIE.document.documentelement.lastchild ;
    #populate the document.body
    $oDocBody.innerhtml=$(Get-MemoryStats | ConvertTo-HTML)
    $oDocBody.style.font="12pt Arial";
    $oIE.document.bgcolor="#D7D7EA"
    
    #Prepare a title.
    $oTitle=$oIE.document.createElement("P")
    $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

    Participant
    Points: 0
    Rank: Member

    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

The topic ‘CPU and MEM usage on regular intervals’ is closed to new replies.