Reputation: 179
I actually have two questions I'm hoping someone can help me with.
The purpose of this script is to give me visibility if machines are running on high memory and CPU consumption. The list of servers is defined in a txt file called ServerList1.txt. The results are dumped to an HTML file.
Please see the code below. When you set your CPU to a value over 70%, the script should display which machine is over the 70% mark. Currently, if I set a machine to 100% CPU, it shows 100$ for ALL machines listed. Any idea as to why? I've been scratching my head at it for some time now.
Related to the above, when a machine hits a value higher than 70%, the table cell should fill with red. right now, it will fill in all the cells with red. I'm guessing once I can figure out why it shows 100% for all the machines, this issue should also correct itself and only highlight the cell as red for a particular machine.
Below is the code: (Yes, it's a little sloppy I'm just trying to get things working for now.)
$ServerListFile = "C:\Monitor\ServerList1.txt"
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue
$GetDate = (Get-Date).ToString("dddd MMMM dd - hh:mm:ss tt")
$Result = @()
ForEach ($ComputerName in $ServerList)
{
### Processor %
$Proc = Get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1
$CPU = ($Proc.Readings -split ":")[-1]
$CPU = [math]::Round($CPU,2)
### Memory %
$Memory = Get-WMIObject -Class Win32_OperatingSystem -Computername $ComputerName | Select-Object @{Name = "MemoryUsage"; Expression = {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize)}}
$Memory = $Memory | Select-Object -ExpandProperty MemoryUsage
### Create custom objects for report
$Result += [PSCustomObject] @{
ServerName = "$ComputerName"
CPULoad = "$($CPU)%"
Memory = "$($Memory)%"
}
### HTML formatting
$Report = "
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=""refresh"" content=""1; URL=Test.htm"" />
<link rel=""shortcut icon"" href=""favicon.png"" type=""image/x-icon"" />
<title> Server Health Report</title>
<style>
html, body {
height: 100%;
background-color: #e6e6e6;
}
html {
display: table;
margin: auto;
}
body {
display: table-cell;
vertical-align: top;
}
p {
font-family: Microsoft Tai le;
}
h1 {
color: rgb(153,0,0);
text-align: center;
font-family: Microsoft Tai le;
font-size: 50px;
}
h2 {
color: rgb(153,0,0);
text-align: center;
font-family: Microsoft Tai le;
font-size: 30px;
}
td {
font-family: Microsoft Tai le;
font-size:24px;
padding:8px 12px;
border-style:solid;
border-width:1px;
/*overflow:hidden;*/
word-break:normal;
border-color:#999;
color:#444;
border-top-width:1px;
border-bottom-width:1px;
}
</style>
</head>
<body>
<h1>Server Health Report</h1>
<h2>$GetDate</h2>
<table border=1 cellpadding=0 cellspacing=0>
<tr bgcolor=#00ace6 align=center>
<td><b>Server Name</b></td>
<td><b>CPU Utilization</b></td>
<td><b>Memory Utilization</b></td>
</tr>"
Foreach($Entry in $Result)
{
IF ($CPU -gt "70" -or $Memory -gt "70")
{
#$Report += "<tr bgcolor=#ff0000>"
$Report += "<trfont-color=#ff0000></font>"
$ReportDate = Get-Date -format "dddd MMMM dd - hh:mm:ss tt"
$ReportName = $Entry.Servername
$ReportCPU = $Entry.CPULoad
<#Write-Output "========== LOG ENTRY ==========" | Out-File C:\Monitor\Testmonitor.txt -Append
Write-Output "Timestamp: $ReportDate " | Out-File C:\Monitor\Testmonitor.txt -Append
Write-Output "Server Name: $ReportName" | Out-File C:\Monitor\Testmonitor.txt -Append
Write-Output "CPU Utilization: $ReportCPU" | Out-File C:\Monitor\Testmonitor.txt -Append
Write-Output "" | Out-File C:\Monitor\Testmonitor.txt -Append
Write-Output "" | Out-File C:\Monitor\Testmonitor.txt -Append#>
}
ELSE
{
$Report += "<tr>"
}
$Report += "<td align=center>$($Entry.Servername)</td>
<td align=center>$($Entry.CPULoad)</td>
<td align=center>$($Entry.Memory)</td>
</tr>"
}
$Report += "</table></body></html>"
}
$Report | Out-File C:\Monitor\Test.htm
Upvotes: 1
Views: 74
Reputation: 9266
This line, just inside the Foreach($Entry in $Result)
loop
IF ($CPU -gt "70" -or $Memory -gt "70")
This is using the temporary variables leftover from the previous loop, which contain data for just the last object in the $Result
array. The line you want is:
IF ($Entry.CPU -gt "70" -or $Entry.Memory -gt "70")
Upvotes: 1