Wednesday, February 05, 2014

Schedule a powershell script

When certain task has run, its nice to be notified. since email function is deprecated in taskscheduler we have to pull out powershell.


1. Create the script


$psemailserver = ""
$receiver =
$from =
$subject = "Task has run"
$body = "Scheduled task $task has run. "
send-mailmessage -to $receiver -from $from -subject $subject -body $body
write-host "Mail sent for $task"
write-eventlog -logname Application -source "my script" -entrytype information -eventid 1 -message "$task has run"

2. Create the call using Task Scheduler “powershell –file “c:\myfile\sendmail.ps1” “helloworld”

What the script does is take an input variable and sending an email using defined smtpserver in the script. When mail is sent, it also logs en event in the application eventlog with source “my script”. The param is optional, but helpful if you want more reusability.

The source needs to created the first time manually, or use an already existing source.

Create source “my script” in application

/>New-eventlog –logname application –source ”my script”


No comments: