Friday 15 July 2016

(CPU usage, Drive utilization, Memory utilization for the servers) using powershell

This script pools server health status (CPU usage, Drive utilization, Memory utilization for the servers).

Below are the script and steps to run the same:
1. Login to the server and open PowerShell ISE or PowerShell with administrator rights.
2. Copy this code to the PowerShell ISE and do modify
3. Click on run button or press F5.
4. Wait till competition of code5. You will get one .htm file to provided location open and verify the same.
Note: make sure you have created server name details on script page.
Below are the screen shots to do:
1. Download attached PowerShell script and save to drive.
2. Modify as we mentioned and save the same.
https://gallery.technet.microsoft.com/site/view/file/156575/1/Capture.PNG
3. Open PowerShell with admin rights and run this command and see the result as below:
https://gallery.technet.microsoft.com/site/view/file/156577/1/Capture.PNG
PowerShell
#################################################################################   
##   
## Server Health Check   
## Created by Prashant Kumar    
## Date : 14 July 2016   
## Email: prashant8590@outlook.com     
## This scripts check the server Avrg CPU and Memory utlization along with C drive   
## disk utilization and sends an email to the receipents included in the script  
################################################################################   
  
$ServerListFile = "E:\prashant\ServerList.txt"    
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue   
$Result = @()   
ForEach($computername in $ServerList)   
{  
  
$AVGProc = Get-WmiObject -computername $computername win32_processor |   
Measure-Object -property LoadPercentage -Average | Select Average  
$OS = gwmi -Class win32_operatingsystem -computername $computername |  
Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}  
$vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" |  
Select-object @{Name = "C PercentFree"; Expression = {“{0:N2}” -f  (($_.FreeSpace / $_.Capacity)*100) } }  
    
$result += [PSCustomObject] @{   
        ServerName = "$computername"  
        CPULoad = "$($AVGProc.Average)%"  
        MemLoad = "$($OS.MemoryUsage)%"  
        CDrive = "$($vol.'C PercentFree')%"  
    }  
  
    $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>Avrg.CPU Utilization</B></TD>  
                       <TD><B>Memory Utilization</B></TD>  
                       <TD><B>C Drive Utilizatoin</B></TD></TR>"  
                          
    Foreach($Entry in $Result)   
      
        {   
          if((($Entry.CpuLoad) -or ($Entry.memload)) -ge "80")   
          {   
            $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>"   
        }   
   
$Outputreport | out-file E:\prashant\Test.htm   


2 comments:

  1. Thanks for the post Prashant, by the way how about the inclusion of E: drive in your Powershell script

    ReplyDelete
  2. Yes, how to get details of D & E drives also?

    ReplyDelete