none
Command runs successfully from a cmd prompt, but not when using Task Scheduler

    Question

  • I have been tasked with automating creating reports and emailing them from a Windows Server 2008 R2 VM.

    I have successfully created a command that uses the ruby executable to pull a report and save it as a pdf file on a local folder on the server.  If I delete the file and run it again using a Scheduled Task, it works again. So, all good there.

    My next step is to email that file and automate that, too. So, I would essentially have 2 scheduled tasks that would be run monthly.  After many tribulations, I have been able to successfully use a sendEmail command at the cmd prompt to email that file to me as a test.  Great! I try to run that same command using the Task Scheduler, and although the Task Scheduler reports all steps were successful and complete, I receive no email. Ugh!

    So... task scheduler can allow a file to be created and placed in a folder but can't send an email?  Is there something I don't know about getting Task Scheduler to run a command even thought it works fine when manually executing the command from a cmd prompt?

    This is Windows Server 2008 R2 on a VM so I remote to it using a domain account. We do not have Exchange. We use an smtp server through Office 365. This machine is strictly used to run the Spiceworks program, which is doing great and even turns incoming emails into tickets and emails ticket notifications without a problem.  The sendemail.exe and the report to be attached are in one folder local to the server.

    What I've tried thus far:

    First tried using Blat for the email sending, but couldn't even get that to work from a cmd prompt.
    Made sure the sendemail command worked with all the desired switches from a cmd line.  I receive the email with attachment within a second or two of executing it.
    I've tried running the scheduled task using a local account as well as my domain account.
    I've checked and unchecked the use with highest priviledges option.
    I've chosen the "config for Windows 2008 R2" as well as "config for Windows 2008".
    I've checked and unchecked the "store password" option.
    I've placed the command in a bat file, confirmed that the bat file works when manually executed, then tried to execute the same bat file using the scheduler, with no luck. When creating the bat file, I simply put the command in a text file and renamed it with a bat extension. I did not add any additional lines of code in it.
    The local security policy allows the user running the task to Log On as a Batch Job.
    Everything I've tried shows as successful in the Task Scheduler, but I never get the email.

    I can't seem to find any helpful or relevant info in the event viewer or in the history details of the task.

    Are there certain reasons why task scheduler is unable to complete this sendemail task even though it works when executed manually?  Are there any error logs that exist or can be created to help out a little more?  Any help or direction is greatly appreciated. 


    Dana Schwehr MCP, A+

    Monday, April 30, 2012 10:54 PM

Answers

  • Hi Dana,

    Okay, so if the "cmd /c" approach didn't work, then we could be looking at something missing from the environment at the time of execution.

    Are you able to provide the contents of the batch file you were trying to run as the scheduled task, when you were trying that approach?

    As far as the account under which it runs goes, I'd stick with the SYSTEM account unless you know for sure you need to use a particular domain account. At least that avoids local permissions issues until we know what we're dealing with.

    Cheers,
    Lain

    Wednesday, May 02, 2012 2:12 AM

All replies

  • Hi Dana,

    This is common with batch files and certain commands, as when they're run from things like Task Scheduler or group policy Scripts, they're run asynchronously by default, which means that quite often the calling process terminates before the actual command has run, and in losing the parent environment, the command often then terminates prematurely.

    Go back to your first command (don't worry about the batch file) and do the following:

    • Edit the action you've defined in the Actions tab
    • Copy the current value of the "Program/script" field
    • In the "Add arguments" field, start the line with a "/c " (without the double quotes, but with the space after the /c) then paste your original "Program/script" value
    • Go back to the "Program/script" field, remove the value entirely and replace it with "C:\Windows\System32\cmd.exe" (or whatever the path to cmd.exe is on your system if it's non-default)

    So, what this should give you is the following:

    • Program/script = C:\Windows\System32\cmd.exe
    • Add arguments = /c <yourOriginalCommandHere>

    The "/c" simply instructs the command shell to wait for the provided command to actually terminate itself before closing, meaning the shell environment exists for the duration of the command rather than dropping out from beneath it.

    Try that and let us know how it turned out. It's possible something else is at play, but this sounds like the most likely scenario.

    Oh, and in terms of what account the scheduled task can run under, the easiest option is just to use the SYSTEM account, though there are some caveats if authenticated network access is required (won't go into that here since it ought not to be relevant in this case).

    Cheers,
    Lain

    Monday, April 30, 2012 11:52 PM
  • In Win2008 the easiest way to email file with task scheduler is to select that action in the task. (no more need for a .exe to send email)

    You can create your scheduled's task, and in the action's pane you make your first action (start a program)

    After, you create another action and you select to send a email.

    You are sure that all action are done in order, and the only thing after that that might block you is under what account you run the task. (system account is perfect if it's not a DC, else if it's a DC you need server operator with special's editing in the local policy, or simply a domain's admin account)


    MCP | MCTS 70-236: Exchange Server 2007, Configuring

    Tuesday, May 01, 2012 2:45 AM
    Moderator
  • Hello,

    I forgot to mention that I'd already tried the "send an email" option in Task Scheduler the first time since I figured that would be easy.  But, it gave an error when trying to run it.  Of course, never enough details to tell me why it couldn't run it.

    Since then, everything I've tried has resulted in all success and no errors as far as the Task Scheduler history is concerned, but the email is never sent.

    Lain's suggestion looked promising. I tried that several ways, using the batch file and not using a batch file, using different user accounts to run it... but still all with the same symptoms.  Task scheduler says all was completed successfully, but no email.  Ugh!

    If there are no other ways to workaround this, I may need to know what some alternative options might be to get this completed.  For example, can I create reports in SQL Reporting Services and have them auto send...?  Of course, I have no reporting tools on that server, and nothing is shared out.

    Is there a way to even view some kind of debug log to get more information as to why this fails?

    Thank you!!

    Dana


    Dana Schwehr MCP, A+

    Tuesday, May 01, 2012 4:14 PM
  • If the SMTP does not work.. you are sure your AV don't scan outgooing email ? Or any other third part program that might interfere.

    Run the task with a domain admin account and select to run it as with the most priviledge to test.


    MCP | MCTS 70-236: Exchange Server 2007, Configuring

    Wednesday, May 02, 2012 1:58 AM
    Moderator
  • Hi Dana,

    Okay, so if the "cmd /c" approach didn't work, then we could be looking at something missing from the environment at the time of execution.

    Are you able to provide the contents of the batch file you were trying to run as the scheduled task, when you were trying that approach?

    As far as the account under which it runs goes, I'd stick with the SYSTEM account unless you know for sure you need to use a particular domain account. At least that avoids local permissions issues until we know what we're dealing with.

    Cheers,
    Lain

    Wednesday, May 02, 2012 2:12 AM
  • Hi,

    Any update?

    Regards,

    Yan Li

    TechNet Subscriber Support

    If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.


    Yan Li

    TechNet Community Support

    Monday, May 07, 2012 2:25 AM
    Moderator
  • Hi,

    In a similar situation, my solution turned to be very hard to find, but easy to implement.

    You need to add a desktop folder to C:\Windows\SysWOW64\config\systemprofile.

    When you are run the application as a scheduled task, for whatever reason, it needs that folder to be there.

    So, once you have C:\Windows\SystWOW64\config\systemprofile\Desktop, hopefully your script will run.


    Jeff


    • Edited by Toxic1 Thursday, March 28, 2013 10:58 PM
    Thursday, March 28, 2013 10:58 PM
  • YOU ARE CORRECT. The folder has to be there before you create the scheduled task however. this issue has baffled me to no resolve until i read your post. Thanks.
    Thursday, August 21, 2014 5:48 PM
  • Sorry I didn't update on this before.  Nice to know about the folder that needs to be created.  Without knowing that and having to move on, I ended up creating an SSIS package to schedule moves of data to a SQL database, then created a data driven SSRS report to do everything we wanted.  Turned out to be better that way anyway and easier to manage just having to update a table with needed parameters for the varying reports created and each sent to appropriate client.

    Thank you!!  Glad Jeff was able to help another individual with the issue. :-)

    Dana


    Dana

    Friday, August 22, 2014 6:19 PM