Powershell script to remotely monitor a Server Process

Here is a simple Powershell script, which will show a Message box, if a process on a remote machine is taking too much memory.

To run the script:
  • install PowerShell 2.0
  • then from the command line:
powershell -F monitor_build_mc.ps1


You can make a batch file which contains the above line, and then run this from a Windows Scheduled Task, to automatically alert you, when a process (like a Web Server) starts hogging the server machine.




#script to monitor a process on remote machine
#
#DEPENDENCIES:  Powershell 2.0, .NET2, possibly also Windows XP SP3
#
#if the process has more than X Kb of RAM, then we will show a message box to warn user!
#
#Author - Sean Ryan - 2011
#Version - 1.0
############################################################
#Command line args
###################
Param([string]$maxNumKb = 450000,            # the maximum number of KB to allow before warning
[string]$procName = "tomcat6w",      #   the name of the process to monitor
[string]$server = "iebuild6"        #   the name of the machine to monitor
)




function Show-InformationBox ([string]$Title,[string]$Message) { 
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null 
    [Windows.Forms.MessageBox]::Show($Message, $Title, [Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information, [System.Windows.Forms.MessageBoxDefaultButton]::Button1, [System.Windows.Forms.MessageBoxOptions]::DefaultDesktopOnly) | Out-Null     
}


$process = [System.Diagnostics.Process]
$processes = $process::GetProcessesByName($procName, $server)


$procData = ($processes)[0]


#to see what members are available, run this:
#Get-Process | Get-Member
$memInBytes = $procData.WS
#$memInBytes
$pmInKb = $memInBytes / 1024
#$pmInKb 


if($pmInKb -gt $maxNumKb)
{
    $msg = "The process $procName is taking $pmInKb Kb of RAM on machine $server !"
    write-host($msg)
    Show-InformationBox("Build Machine Monitor", $msg)
}

Comments