Email notification if scheduled task runs too long

We have a Server 2012 machine that runs a batch file as a scheduled task daily at 00:30 (it synchronises data to other company sites).  We want to set up an email notification that is triggered if the task has not completed by 03:30.  We do not want to stop or restart the task - just to know if it's still running.

How can we go about setting this up?

Thanks in advance.
David HaycoxAsked:
Who is Participating?
Rob StoneConnect With a Mentor Commented:
You can create a new scheduled task to run at 03:30. Under Actions select PowerShell.exe as the executable to run and in the Add Arguments field put the full path to the powershell script.

Find the taskname of your scheduled task by typing Get-ScheduledTask in powershell and complete the script below.

Then try the code below:
$Task = Get-ScheduledTask -TaskName "ENTER TASK NAME HERE"
$Taskname = $Task.TaskName
IF ($Task.State -eq "Running"){Send-MailMessage -To "emailaddress" -From "" -Subject "$Taskname is still running!" -SmtpServer "SMTP Server"

Open in new window

David HaycoxAuthor Commented:
That certainly looks on the right lines, but could be unnecessarily complex for our scenario.  The suggested script checks for a task running for more than a certain time; for us simply checking whether the task is running at 03:30 would be sufficient.

Also it doesn't run as is, giving multiple errors as follows:

Cannot convert value "N/A" to type "System.DateTime". Error: "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."

Could this be related to regional settings?  In any case it's not relevant as I plan  to use the email sending part of the script above, but am looking for something to simply check the status of the task at the specified time.
David HaycoxConnect With a Mentor Author Commented:
Stoner79: the code you posted works a treat on Windows 8, but when I try it on Server 2012 (with the PowerShell pack installed) I get the error below.

Get-ScheduledTask : A parameter cannot be found that matches parameter name 'TaskName'.
At C:\Scripts\MonitorTask.ps1:1 char:27
+ $Task = Get-ScheduledTask -TaskName "Test Task"
+                           ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ScheduledTask], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Get-ScheduledTask

Open in new window

So it looks like "-TaskName" is incorrect under Server 2012, it should be "-Name".  Also it's "Status" not "State".  So, I amended your code as follows:

$Task = Get-ScheduledTask -Name "ENTER TASK NAME HERE"
$Taskname = $Task.Name
IF ($Task.Status -eq "Running") {Send-MailMessage -To "emailaddress1","email address2" -From "" -Subject "$Taskname is still running!" -SmtpServer "SMTP Server"}
IF ($Task.Status -eq "Running") {Write-Host Still Running} ELSE {Write-Host Not running}

Open in new window

(the fourth line is just for testing so you can see what it's doing, plus I added a second email address)

This works well, so next I want to schedule this at 03:30 daily, so I added a basic task in the scheduler to run the script (C:\Scripts\MonitorTask.ps1").  When I run this though, it says "running" until but doesn't do anything.  How should I configure it so the script runs automatically at the scheduled time?

Thanks again - looking good so far!
WEBINAR: GDPR Implemented - Tips & Lessons Learned

Join the WatchGuard team on Thursday, March 29th as we recount some valuable lessons learned in weighing the needs of a business against the new regulatory environment, look ahead at the two months left before implementation, and help you understand the steps you can take today!

David HaycoxConnect With a Mentor Author Commented:
Ah - found it.  Here's the syntax:

powershell.exe "& 'C:\Scripts\MonitorTask.ps1'"

So "powershell.exe" goes in the "Program/script" field and the rest in the "add arguments" field.

Works great!
Rob StoneCommented:
Cool. Sorry it didn't work from the off!  Not sure why it didn't work for you in 2012, I used it on 2012 using Amazon's image in AWS.

I was using the scheduled task against a 2008 core server, so that's good to know the & and "" marks are needed for 2012 and PowerShell scripts!
David HaycoxAuthor Commented:
Worked great in Windows 8 but had to make a couple of minor tweaks myself to work in Server 2012.  Excellent!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.