Reputation: 33
I am writing script to monitor servers and get the result in html when threshold is crossed for memory,cpu and cdrive. My script is running but I am not getting accurate highlighted rows when condition is met in if/else statement.
# Path for the CSV file that contains all the Print Server information.
$ServerDetails=import-csv "C:\cn.csv"
$result=@()
foreach($server in $ServerDetails){
try{
#sysinfo variable contains complete systeminfo like manufacturer name, physical memory,servername
$cpu=Get-WMIObject -ComputerName $server.servers win32_processor| select __Server, @{name="CPUUtilization" ;expression ={“{0:N2}” -f (get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 |
select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average}}
$disks =Get-WmiObject -Class win32_Volume -ComputerName $server.servers -Filter "DriveLetter = 'C:'" |
Select-object @{Name = "PercentFree"; Expression = {“{0:N2}” -f (($_.FreeSpace / $_.Capacity)*100) } }
$os=gwmi -Class win32_operatingsystem -computername $server.servers |
Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
$result += [PSCustomObject] @{
ServerName ="$($server.servers)"
CPULoad = $cpu.CPUUtilization
CDrive = $disks.PercentFree
MemLoad = $OS.MemoryUsage
}
}
catch{
"error communicating with $($server.servers), skipping to next"
}
$Outputreport = "<HTML><TITLE> Server Health Report </TITLE>
<BODY background-color:peachpuff>
<font color =""#99000"" face=""Microsoft Tai le"">
<H2> Server Health Report </H2></font>
<Table border=1 cellpadding=0 cellspacing=0>
<TR bgcolor=gray align=center>
<TD><B>Server Name</B></TD>
<TD><B>CPULoad</B></TD>
<TD><B>Memory Utilization</B></TD>
<TD><B>CDrive</B></TD></TR>"
Foreach($Entry in $Result)
{
if(($Entry.CDrive) -le "65")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
if(($Entry.MemLoad) -ge "70")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
if(($Entry.CPULoad) -ge "15")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.Cdrive)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
$out=$Outputreport | out-file C:\Test.htm
}
}
for ex:-my cpu threshold is set for greater than equal to 15, but I am getting rows in red where none of the threshold condition is met or only one of them either of memory or Cdrive is met. Any help is appreciated.
Upvotes: 1
Views: 451
Reputation: 11364
Replace the foreach loop with this.
Foreach($Entry in $Result)
{
if([decimal]$Entry.CDrive -le 65 -or [decimal]$Entry.MemLoad -ge 70 -or [decimal]$Entry.CPULoad -ge 15)
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.Cdrive)</TD></TR>"
}
Issue and Fix
You are comparing string with strings when comparing the Entry field with certain threshold. You need to compare the numbers with numbers, decimal in this case. If you dont use [decimal] before the Entry's field, then it compares it with its default type of String, which doesnt work.
Following is the test I performed,
$result.CPULoad # output: 9.74
$result.CPULoad -gt "10" # output True
[decimal]$result.CPULoad -gt 10 # output False
[decimal]$result.CPULoad -gt 9.74 # False
[decimal]$result.CPULoad -gt 9.73 # True
UPDATE
If you are looking to get % Bytes In Use (Memory Utilization) as well, you can run the following command
$mem = Get-WMIObject -ComputerName server.servers win32_processor| select __Server, @{name="MemUtilization" ;expression ={“{0:N2}” -f (get-counter -Counter "\Memory\% Committed Bytes In Use" -SampleInterval 1 -MaxSamples 5 |
select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average}}
Upvotes: 0