Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for running processes and email 1

Status
Not open for further replies.

MTBarry

IS-IT--Management
Sep 24, 2012
4
US
Hello.

I'm trying to write a script that will check for a handful of running processes. Say....5. And if just one of them is not running, it will send me an email.

Thanks for any help.
 
Hi. So this will check for a single running process. But I haven't been able to get something working that will check for multiple processes and take action if only one is not running.

Code:
Dim AllProcess 
Dim Process 
Dim strFoundProcess
strFoundProcess = False 
Set AllProcess = getobject("winmgmts:") 'create object 
For Each Process In AllProcess.InstancesOf("Win32_process") 
If (Instr (Ucase(Process.Name),"TASKMGR.EXE") = 1) Then 
		msgbox "Application is running" 
		strFoundProcess = True 
Exit for 
		
End If
Next 
If strFoundProcess = False Then 
msgbox "Application is not running" 
End If
Set AllProcess = nothing

I also found a SendEmail function that I was going to plug in as the action to take if just one process isn't running. I haven't messed with it yet, but here is what I'm planning on using.

Code:
Sub SendAlertEmail()
    Set pc = CreateObject("Wscript.Network")
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields
 
    Const schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    dim emailBody
 
    Flds.Item(schema & "sendusing") = 2
    Flds.Item(schema & "smtpserver") = "143.323.xxx.xxx" 
    Flds.Item(schema & "smtpserverport") = 25
    Flds.Item(schema & "smtpauthenticate") = 0
    'Flds.Item(schema & "sendusername") = "domain\username"
    'Flds.Item(schema & "sendpassword") = "yourpass"
    Flds.Item(schema & "smtpusessl") = 0
    Flds.Update
 
    With iMsg
        .To = mail@mail.com, mail2@mail.com"
        .From = "Process Alert <administrator@mail.com>"
        .Subject = processName & " has quit"
 
        emailBody = "<font color='red'><h2>'" & processName & "' has unexpectedly quit</h2></font>"
        emailBody = emailBody & "<b>Server: </b>" & pc.ComputerName & "<br />"
        emailBody = emailBody & "<b>Process: </b>" & processName & "<br />"
        emailBody = emailBody & "<b>Location: </b>" & processPath & processName & "<br />"
        emailBody = emailBody & "<b>Time: </b>" & Now & "<br /><br />"
        emailBody = emailBody & "<b>Attempting to restart '" & processName & "'</b><br /><br />"
        emailBody = emailBody & "<font color='#808080'>Please check that the process has restarted correctly.<br />" 
        'emailBody = emailBody & "

        .HTMLBody = emailBody
        .Sender = "Process Alert <administrator@mail.com>"
        .Organization = "Your Company"
        .ReplyTo = "replyhere@mail.com"
 
        Set .Configuration = iConf
 
        .Send 
    End With   
 
    ' Release Interfaces
    Set iMsg = nothing
    Set iConf = nothing
    Set Flds = nothing
End Sub
 
Might be easier to have a separate function to compare each running process with your list. Many ways to do it, here's one:

Code:
Function ProcessIsInList(sProcessToCheck)
   Dim arrProcessList, sProcess
   arrProcessList = Array("TASKmgr.exe","progr1.exe","prog2.exe")
   For Each sProcess in arrProcessList
      If UCase(sProcess) = UCase(sProcessToCheck) Then
         ProcessIsInList = True
         Exit Function
      End If
   Next

   ProcessIsInList = False
End Function

So, this line:
If (Instr (Ucase(Process.Name),"TASKMGR.EXE") = 1) Then
could be replaced with:
If ProcessIsInList(Process.Name) Then

 
That's great. Thanks. But it's working the opposite way I'm looking for. Ultimately, if just one of many processes is not running, I need it to send me an email. What I've got here is a script that will be happy if only one of many processes is running.

Code:
Dim AllProcess 
Dim Process 
Dim strFoundProcess
Dim count
strFoundProcess = False 
Set AllProcess = getobject("winmgmts:")  
For Each Process In AllProcess.InstancesOf("Win32_process") 
If ProcessIsInList(Process.Name) Then 
		count = count+1
		msgbox "Found " & count
		strFoundProcess = True 
Exit For 
		
End If
Next 
If strFoundProcess = False Then 
msgbox "Application is not running" 
End If
Set AllProcess = nothing

Function ProcessIsInList(sProcessToCheck)
   Dim arrProcessList, sProcess
   arrProcessList = Array("taskmgr.exe", "winword.exe", "firefox.exe")
   For Each sProcess in arrProcessList
      If UCase(sProcess) = UCase(sProcessToCheck) Then
         ProcessIsInList = True
         Exit Function
      End If
   Next

   ProcessIsInList = False
End Function
 
Whoops. I forgot I still had that Exit For line in there. I pulled it out and added a count line so if it doesn't find the right number of processes it sends the email. I think it's going to work. I haven't tried the emailing bit, but I think I can get that. Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top