Restart Service to avoid Process Handle Leaks

Hi Readers,

Just sharing a script that We have written to cater a situation that we are facing with ccmexec.exe process.

We are facing event id 5719 on our servers , servers loss connection to Domain, shares & other network related things.

Rebooting was solving the issue but working with MS we have found that there is some bug in ccmexec.exe & as a result of that its

handles get increase beyound control which causes this issue.

Download the zip file  from below link & change the variables accordingly.

https://gallery.technet.microsoft.com/scriptcenter/Restart-Service-if-Process-5fffe6b8

#####################################################

$fromadd = “SystemsMonitoring@labtest.com”

$email1=”Vikas@labtest.com”

$email2=”sukhija@labtest.com”

$smtpServer=”smtp.labtest.com”
$process = “CcmExec”

$handlelimit = “6000”

########################################################

$server_list=@(“Server01″,”Server02″,”Server03″,”Server04″,”Server05″,”Server06″,”Server07”)

##########################################################

Schedule the batch file.

#################################################################### 
#       Author: Vikas Sukhija 
#       Date: 03/17/2015 
#       Description: Check for CCMexe handle counts 
#    Restart if High handle count is detected 
#################################################################### 
 
$fromadd = “SystemsMonitoring@labtest.com” 
$email1=”Vikas@labtest.com” 
$email2="sukhija@labtest.com" 
$smtpServer="smtp.labtest.com" 
 
$process = "CcmExec" 
$handlelimit = "6000" 
 
 
$server_list=@("Server01""Server02""Server03""Server04""Server05""Server06""Server07") 
 
$server_list | foreach-object { 
$host1 = $_ 
$data = @() 
$handles = Get-Process -Name $process -ComputerName $_ 
 
$handlecount = $handles.handles 
 
 
if($handlecount -ge$handlelimit) 
 { 
Write-host "Handle count $handlecount for $process is above limit $handlelimit" -foregroundcolor Red 
Write-host "Restart $process on server $_" -foregroundcolor Green 
     
Restart-Service -InputObject $(Get-Service -ComputerName $_ -Name $process) 
 
 
#########################send email if any error occurs####################### 
 
if ($error -ne $null) 
      { 
 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 
 
#Mail sender 
$msg.From = $fromadd 
#mail recipient 
$msg.To.Add($email1$msg.To.Add($email2$msg.Subject = "Auto restart error for service $process for High handle utilization on  $host1" 
$msg.Body = $error 
$smtp.Send($msg) 
       } 
 
else 
      { 
 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 
 
#Mail sender 
$msg.From = $fromadd 
#mail recipient 
$msg.To.Add($email1$msg.To.Add($email2$msg.Subject = "Auto restart successful for service $process High handle utilization on  $host1" 
$msg.Body = "Service $process  has been restarted on $host1" 
$smtp.Send($msg) 
 
} 
 
############################################################################# 
 
} 
 
Else 
 
{ 
Write-host "Handle count $handlecount for $process is below limit $handlelimit on server $host1" -foregroundcolor Green 
 
} 
} 
 
############################################################################## 

Tech Wizard

https://techwizard.cloud

https://syscloudpro.com/

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s