Author Posts

August 20, 2015 at 10:19 am

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

August 20, 2015 at 11:10 am

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)
}

August 21, 2015 at 2:59 am

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