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!
If you try to even go further in the wizard, it refuses to accept and dialogues out as ‘deprecated feature’.
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.
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.
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)
You may also like Cloud Infrastructure & Management
Create a basic task
Select When A specific event Logged
Select and furnish any trigger for which you want e-mail alert
On next screen select “Start a program”
In program/script à type powershell.exe à In argument give the path of EmailScript.PS1 with file name
Check Open Property Dialog à Now Click Finish
Check over the below options matches
Similarly you can put multiple triggers of Event IDs
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.
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
Hi, great script! Is it possible to add an attachment?
Thanks!
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
is there anyway we can send an attachment with this email?
I want to send a report everyday to my team via email attachment
Thanks
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
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
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
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?
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
Task schedule not run automatically. Kindly guide me
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
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.
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
Hi,
I am using the MySQL Database.
Regards,
Ali Asgar
i tried to add those 3 line but didn’t work.
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = “SMTP.@@@@.com”
$mailmessage.from = (“XXXX@XX.com”)
$mailmessage.To.add(“frac@XXX.com”)
$mailmessage.Subject = “Alert Subject”
$mailmessage.Body = “Alert Body with Instruction to recipients”
$file = Read-Host -Prompt “C:\dbtransfer\output”
$attach=new-object system.web.mail.mailattachment($file,”uuencode”)
$mailmessage.Attachments.Add($attach)
$smtpclient.Send($mailmessage)
Well done … don’t know why Microsoft keep removing things people clearly have a need for and then don’t explain the new workaround … smacks of arrogance to me!
Anyway, if I am using Google (smtp.gmail.com:587) as my SMTP server how can I send my password to Google?
When I create the task, it generates many emails within 1 minute even though my trigger only occurred once. Do you know what could be causing this
Me to. Many email within 1 minute
Hi, How to configure this for a specific task. using the Event ID will return for all the task
Script worked great, however I did receive multiple emails that did not stop sending until I opened the scheduled task and disabled the task. Is there a reason why I am reaching none stop email when this script is run?
you ever get this fixed?