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 HaycoxConsultant EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David HaycoxConsultant EngineerAuthor 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.
Rob StoneCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David HaycoxConsultant EngineerAuthor 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!
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

David HaycoxConsultant EngineerAuthor 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 HaycoxConsultant EngineerAuthor Commented:
Worked great in Windows 8 but had to make a couple of minor tweaks myself to work in Server 2012.  Excellent!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2012

From novice to tech pro — start learning today.