Check if a Process is running. If not start it and send an Email


Get-Process (gps) Gets the processes that are running on the local computer or a remote computer.

# 
# =======PowerShell Script========================================
# COMMENT: Check if Process is running. If not start it and send an Email
$myproc = "program my process.exe"
if (!(gps myproc -ea 0)) { 
    $MyProcCMD = "c:\program files\$myproc /switches"  #<-- cmd to start proc
    $wmi = ([wmiclass]"win32_process").Create($MyProcCMD) 
    if ($wmi.returnvalue -eq 0) { 
        $smtp = new-object system.net.mail.smtpClient("smtp.domain.com") 
        $mail = new-object System.Net.Mail.MailMessage 
        $mail.from = "someone@domain.com" 
        $mail.to.add = "someone else@domain.com"
        $mail.subject = "MyProc was in Stopped state. Successfully restarted." 
        $mail.body = "bla-bla-bla"
        $mail.IsBodyHtml = $true 
        $smtp.send($mail) 
		}
    } 
	
############# End POSH Script ###############
Advertisements

5 thoughts on “Check if a Process is running. If not start it and send an Email

  1. Whats up very nice website!! Man .. Beautiful .. Amazing .. I will bookmark your site and take the feeds additionally?I am happy to seek out a lot of useful information here in the publish, we need develop extra strategies in this regard, thanks for sharing. . . . . .

  2. Simply wish to say your article is as surprising.
    The clarity in your post is just cool and i could assume you are an expert on this subject.
    Fine with your permission let me to grab your feed to
    keep up to date with forthcoming post. Thanks a million and please continue the rewarding work.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s