Get SQL Server agent Service Stopped Mail Notification

Implementation: Get a mail alert via powershell when SQL server agent service gets stopped.

Scenario: When SQL server agent service gets stopped,its dependence jobs,DR,operators,DB mail,replication failed to succeed.In these case,you can have a mail alert which will notify whenever agent service gets stopped by triggering mail to provided mail id via windows powershell.

Infrastructure:The below query/steps have been run/tested successfully on SQL server 2005,2008,2008 R2 and 2012.


First write the Powershell code below in notepad and save in location say F:\   drive with name “WorkingPowershellScripts.ps1” without quote.

#Create a .net mail client

#Get the Machine Name


#Put the recepient name.Put only one recepient here.

$recpts = “”

#Enter your SMTP mail server name in place of

$smtp = new-object Net.Mail.SmtpClient(“”)

$subject=”Microsoft SQL Agent Service is down on Server” + ” ” + $SrvrName

#Put the from address


$body=”Dear Team,`n`nPlease Check the SQL Agent Service Status On”+ ” “+   $SrvrName+”.”+” For any assistance plz mail us on “+”`n`nThanks and Regards,`nDharmesh Mishra`nClover TSG-SQL Consultant`nCell No 7506268860”

foreach ($recpt in $recpts){

$msg = New-Object

$msg.From = $from

#Here you can add as many as mail id example



$msg.Subject = $subject

$msg.Body = $body



Copy the above code from below location.


Step 2:

Create one batch file to run above .PS1 file

Open notepad and copy the code below and save in drive say F:\ with name “RunPSByCMD.bat” with quote.


SET ThisScriptsDirectory=F:\

SET PowerShellScriptPath=F:\WorkingPowershellScripts.ps1

PowerShell -NoProfile -ExecutionPolicy Bypass -Command “& {Start-Process PowerShell -ArgumentList ‘-NoProfile -ExecutionPolicy Bypass -File “”%PowerShellScriptPath%””‘ -Verb RunAs}”;

Copy the above code from below location.

Step 3:

Now go to task scheduler and create basis task. Follow the screen shot below.

Create a basic task.


Name the task and description(Optional)