April 28, 2017

“Send an e-mail”-Windows Server 2012 Task Scheduler deprecated feature [SOLVED!]

16622 Views
“Send an e-mail”-Windows Server 2012 Task Scheduler deprecated feature [SOLVED!]

One of most common and lethal tool windows administrator is aware of when you want to send an automated email from task scheduler. However if you ever tried to set a windows task which will required to send an email in Windows server 2012, you will soon realize that it has been deprecated.

When you try to create a task through the task scheduler wizard, it does shows you option “Send an e-mail” but note what’s there is the bracket!

image1

If you try to even go further in the wizard, it refuses to accept and dialogues out as ‘deprecated feature’.

image2

Well you certainly have the most trusted PowerShell to salvage.

Resolution:

We will work here with both the windows task manager feature and PowerShell script, where task will be created in Task manager and email will be sent using  “ Run a Program/Script “  option in Action stage of setup.

Here we will see how to send email using Windows PowerShell and configure a Task in Windows Task Manager and setup email notification.

  1. Open notepad > Copy below Script > Change the highlighted with your details > save the file as PS1. e.g.  “EmailScript.PS1”
    $SmtpClient = new-object system.net.mail.smtpClient
    $MailMessage = New-Object system.net.mail.mailmessage
    $SmtpClient.Host = "SMTP.YOURcompany.com"
    $mailmessage.from = ("DoNotReply_ToThisEmail@ YOURcompany.com ")
    $mailmessage.To.add(" Person@ YOURcompany.com , Person2@ YOURcompany.com, …… ")
    $mailmessage.Subject = “Alert Subject
    $mailmessage.Body = “Alert Body with Instruction to recipients
    $smtpclient.Send($mailmessage)
  2. Create a basic task image3
  3. Select When A specific event Logged image4
  4. Select and furnish any trigger for which you want e-mail alert image5
  5. On next screen select “Start a program” image6
  6. In program/script à type powershell.exe à In argument give the path of EmailScript.PS1 with file name image7
  7. Check Open Property Dialog à Now Click Finish image8
  8. Check over the below options matches image9 image10 image11
  9. Similarly you can put multiple triggers of Event IDs image12

15 Replies to ““Send an e-mail”-Windows Server 2012 Task Scheduler deprecated feature [SOLVED!]”

  1. i want to send automatic email notification on every sunday morning 10 o clock from windows server 2012.

    there i have to attach a list of document expiring in certain dates through a database query.

    please suggest me how to make it.

    1. Hi Ali,

      If you are using SQL Server Database , you have to create a New Agent job, in “Steps” section provide similar TSQL commands and schedule it during “Scheduling” section.

      Example : Below for sending system info email

      EXEC msdb.dbo.sp_send_dbmail
      @profile_name = ‘Main DB Mail profile’,
      @recipients = ‘XYZ@netwoven.com’,
      @subject = ‘Memory Values’,
      @query = N’SELECT total_physical_memory_kb, available_physical_memory_kb, system_memory_state_desc, collection_date_time
      FROM MemoryDMVHistory
      WHERE CAST(collection_date_time AS Date) = CAST(GETDATE() AS DATE)
      ORDER BY collection_date_time DESC;’,
      @attach_query_result_as_file = 1,
      @query_attachment_filename = ‘Memory Values.txt’

      Hope this helps

      -DM

    1. Hi kashif,

      Providing below some checklist that may require a recheck on task schedular to run as expected.

      1. Open your task and under “General” tab, check if you have chosen radio button ” Run Whether user is logged on or not” option and be sure to have “Unchecked” the checkbox ” Donot store password ”
      2. “Check” mark “Run with highest privilege”
      3. Under “Trigger” tab the scheduled status is enabled.
      4. Under “Condition” Tab all boxes unchecked
      5. Under “Settings” tab only check first box ” Allow task to be run on demand” . keep all other boxes unchecked.
      6. When Saving make sure User acccount used for running task is an local admin user on the server or Domain admin for domain controller.

      Hope this Helps

      Regards
      -DM

  2. This will be very helpful. If I use an outside SMTP server is there a way to send login information to be able to send the email?

    1. Hi Matt,

      Assuming you want to pass credentials to an external SMTP server for emailing purpose which is already have proper ports and IP (static) access to and from your perimeter network.

      You would need a SMTP relay server in your perimeter network to access the external SMTP using a Communication ID for the purpose.

      The communication flow would be like
      Source Server  Perimeter SMTP Relay server External SMTP server

      Hope this helps

      Regards
      -DM

  3. thanks for your clear explanation…. I tried adding $mailmessage.attachment=”file_path”.. but it gives the below error. Please help me to fix this.

    The property ‘attachment’ cannot be found on this object. Verify that the property exists and can be set.
    At line:1 char:1
    + $mailmessage.attachment=”G:\oradata\datafiles\REQ0212764\Output\REQ0212764.csv”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

    1. Hi Ramesh,

      For attaching file in alert email, please add the below 3 lines in script in between line#7 and #8 in below format

      $file = Read-Host -Prompt “Enter the folder path”
      $attach=new-object system.web.mail.mailattachment($file,”uuencode”)
      $mailmessage.Attachments.Add($attach)

      Please see if this helps.

      Regards,
      -DM

    1. Hi Khan,

      For attaching file in alert email, please add the below 3 lines in script in between line#7 and #8

      $file = Read-Host -Prompt “Enter the folder path”
      $attach=new-object system.web.mail.mailattachment($file,”uuencode”)
      $mailmessage.Attachments.Add($attach)

      Please see if this helps.

      Regards,
      -DM

    1. Hi Andrea,

      For attaching file in alert email, please add the below 3 lines in script in between line#7 and #8

      $file = Read-Host -Prompt “Enter the folder path”
      $attach=new-object system.web.mail.mailattachment($file,”uuencode”)
      $mailmessage.Attachments.Add($attach)

      Please see if this helps.

      Regards,
      -DM

  4. Thank you so much!!!, have been looking for this for a week, have tried everything to make old command work, didnt know that its not compatible anymore with tasks scheduler on Win 2012.

    1. Hi Adel,

      Good to know you are benefited by this blog. Please keep looking our other blogs and tech talks as well for great info and helpful topics.

      Regards,

      -DM

Leave a Reply

Your email address will not be published. Required fields are marked *