Tuesday 10 September 2013

Monitoring an application with powershell script

This will help you to create a powershell script to check a running application. If the application is running you will receive an email and if its not running powershell script will start the application. You can copy and make the necessary changes to it and run it with power shell, also you can run it as a scheduled task. Save the script as "anyname.ps1". In this example i am trying to see Outlook is running.


$process = Get-Process -Name "outlook"
$date = Get-Date
if (!$process){
{$_.running -eq $false}
    & "C:\Program Files (x86)\Microsoft Office\OFFICE11\outlook.exe"
}
else {

$emailSmtpServer = "smtp.gmail.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "youremailidhere@gmail.com"
$emailSmtpPass = "yourgmailpasswordhere"

$emailFrom = "youremailidhere@gmail.com"
$emailTo = "youremailidhere@gmail.com"

$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = "Computer started with Axis"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
<p>type a message here for you to see on your email.</p>
<p>starting Outlook $date</p>
"@

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )
}

No comments:

Post a Comment