Script to Server Utilization Check

This Script is useful to check the Server Utilization Like CPU,Memory,Disk Utilization and get Warning Alerts by Email

PowerShell Script

### This Script will give you the CPU, Memory and Disk Utilization ###

$ServerListFile = “C:\ServerList.txt”   // Text file with Server names

$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue

$Result = @()

ForEach($computername in $ServerList)   //Fetching each server from the text file

{

$AVGProc = Get-WmiObject -computername $computername win32_processor |

Measure-Object -property LoadPercentage -Average | Select Average // Fetching CPU Utilization

$OS = gwmi -Class win32_operatingsystem -computername $computername |

Select-Object @{Name = “MemoryUsage”; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize – $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }} // Fetching Memory Utilization

$vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter “DriveLetter = ‘C:'” |

Select-object @{Name = “C PercentFree”; Expression = {“{0:N2}” -f  (($_.FreeSpace / $_.Capacity)*100) } } //Fetching C Drive Utilization

$vol1 = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter “DriveLetter = ‘D:'” |

Select-object @{Name = “D PercentFree”; Expression = {“{0:N2}” -f  (($_.FreeSpace / $_.Capacity)*100) } } //Fetching D Drive Utilization

$vol2 = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter “DriveLetter = ‘E:'” |

Select-object @{Name = “E PercentFree”; Expression = {“{0:N2}” -f  (($_.FreeSpace / $_.Capacity)*100) } }  //Fetching E Drive Utilization

$vol3 = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter “DriveLetter = ‘F:'” |

Select-object @{Name = “F PercentFree”; Expression = {“{0:N2}” -f  (($_.FreeSpace / $_.Capacity)*100) } } //Fetching F Drive Utilization

$result += [PSCustomObject] @{

        ServerName = “$computername”

        CPULoad = “$($AVGProc.Average)%”              //Storing the CPU Utilization to CPULoad

        MemLoad = “$($OS.MemoryUsage)%”          //Storing the Memory Utilization to MemLoad  

        CDrive = “$($vol.’C PercentFree’)%”                //Storing the C Drive Utilization to CDrive

        DDrive = “$($vol1.’D PercentFree’)%”            //Storing the D Drive Utilization to DDrive

        EDrive = “$($vol2.’E PercentFree’)%”              //Storing the E Drive Utilization to EDrive

        FDrive = “$($vol3.’F PercentFree’)%”              //Storing the F Drive Utilization to FDrive  

 }

    $Outputreport = “<HTML><center><TITLE> Server Health </center> </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>Avg.CPU Utilization</B></TD>

                       <TD><B>Memory Utilization</B></TD>

                       <TD><B>OS Drive Free Space</B></TD>

                       <TD><B>Top Management Drive Free Space</B></TD>

                       <TD><B>Middle Management Drive Free Space</B></TD>

                       <TD><B>Lower Management Drive Free Space</B></TD></TR>”

//Creating Table

    Foreach($Entry in $Result)

        {

          if((($Entry.CpuLoad) -ge “80”) -or (($Entry.memload) -ge “80”)-OR  (($Entry.cdrive) -le “20”))

//Checking the utilization

// now it will check the drive details if it is less than 20% and we need to configure drive as we required.

          { 

            $Outputreport += “<TR bgcolor=red>”  

 // if utilization is higher than given value result will display as Red

          }

          else

           {

            $Outputreport += “<TR bgcolor=limegreen>”

          }

// if utilization is normal value result will display as Green      

 $Outputreport += “<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.Cdrive)</TD><TD align=center>$($Entry.Ddrive)</TD><TD align=center>$($Entry.Edrive)</TD><TD align=center>$($Entry.Fdrive)</TD></TR>”

        }

     $Outputreport += “</Table></BODY></HTML>”

        } // Displaying the Result

$Outputreport | out-file C:\Scripts\Test.htm   // Creating Html file of current report

Invoke-Expression C:\Scripts\Test.htm   // automatically invoking the result (optional)

$smtpServer = “mail.mstechnets.com”

$smtpFrom = “administrator@mstechnets.com”

$smtpTo = “user1@mstechnets.com”

$messageSubject = “Servers Health report”

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto

$message.Subject = $messageSubject

$message.IsBodyHTML = $true

$message.Body = “<head><pre>$style</pre></head>”

$message.Body += Get-Content C:\scripts\test.htm

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)

$smtp.Send($message)

//Giving Report as Email

Sample Output

Here Result is Red for EXCH-16 because Memory utilization is Very high than the threshold we have given, and those servers who Don’t have extra drives it will just mark %. So that user can add or remove drive easily

Leave a Reply