I have a script that has been modified from other forums to check a process and start it if it's not running. After it starts the program it sends and email, I don't know how to get it to send an email only if it has to start the program... if it's already running I don't want it to send an email. I am very new to power shell and don't understand a lot of the functions, any help would be appreciated.
Thanks,
This is copy of what i'm using to test
# PowerShell script with e-mail notification
# Change these values
$myproc = "SwithMail.exe"
if (!(gps SwithMail -ea 0)) {
$MyProcCMD = "C:\SwithMail" #<-- cmd to start proc
$wmi = ([wmiclass]"win32_process").Create($MyProcCMD)
if ($wmi.returnvalue -eq 0) {
}
}
$EmailFrom = "me@me.com"
$EmailTo = "me@me.com
$EmailBody = "Process test to start process and send email."
$EmailSubject = "Process Test"
$Username = "any@any.com"
$Password = "password"
# Send E-mail message with log file attachment
$Message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $EmailBody)
$SMTPClient = New-Object Net.Mail.SmtpClient("smtp.gmail.com", 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$SMTPClient.Send($Message)