On Unix systems, the scheduled task service will email any output that a scheduled task produces. If no output is produced, no email is sent. How can I do the same thing on Windows Server 2003 or 2008? Is there a way to call a batch file or executable that will run my task, capture the output, and email it only if there is output?

If you propose a PowerShell solution, please note that I only have PowerShell 1.0.


I use autoIT for this type of stuff. It is very easy to use and has this functionality built in. The help files are full of examples and snipits that do exactly what you are looking for. If I had more detail regarding the task at hand I would have a more specific answer.

You should be able to locate what you need from here.


At least on Windows Server 2008, you can use the following route :

  1. Set up your regular task using the task scheduler. If you need to capture the output of your program, I'm assuming it's a command line program. You can redirect command output on the Windows command line to a file just like you can on Unix systems. For that, I would write a batch that runs my command and captures its output:

    REM Run command
    mycmd.exe > outfile.txt
    REM Check that outfile exists
    IF NOT EXIST outfile.txt (
        @echo File does not exist
        exit 2
    REM Compare outfile to a known empty file
    TYPE nul >  blank.txt
    FC outfile.txt blank.txt > nul
    REM Check result, and remove blank file.
    IF errorlevel 1 (
        del blank.txt
        @echo File is not empty
        exit 0
    ) ELSE (
        del blank.txt
        @echo File is empty
        exit 1
  2. Scheduled tasks get logged in the Applications And Services Event Log, under Microsoft > Windows > Task Scheduler > Operational.

    You can then create a second task that gets raised when an entry is added in that log.

    You have to set the action's trigger accordingly. What you need to do is create an event filter on 201 events (Action ended), whose tasks names match the name of your task that runs the above script, and whose result code is 0. It should be possible to setup with the task scheduler's UI, or you can use XPath filtering.

    Then set the action of that task to "Send an email", and set whatever params you need. Set the name of the attached file to the path of outfile.txt, and you're done.

  3. You can use the same technique to handle return codes 1 and 2, to warn of an empty or missing outfile if necessary.

Quite easy, no ? :D Makes me love Linux more and more everyday.

Edit If you have a mailer program that works from the command line, you could simply call it from the script that runs the command, eliminating the need to create the event-based task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.