none
Task Scheduler does not create file.txt RRS feed

  • Question

  • My .exe file is located in C:\users\me\desktop

    When run it creates file.txt in c:\users\me\desktop.

    When run from Task Scheduler it runs but does not create the file.txt.

    Run whether user is logged on or not is checked

    Run with highest privileges is not checked (if it is checked program does not run).

    Configured for Windows Server 2008 R2

    Hope someone can solve this issue.

    Stephen

    <button class="c-button msgReplyBtn" data-bi-id="msgReplyBtn" title="Reply to this post" type="submit">Reply </button>
    <button class="c-button f-lightweight voting-button thdJoinBtn disabled " data-bi-id="msgVoteBtn" data-votecount="0" disabled="disabled" threadtype="QnA" title="This is your question">I have the same question (0) </button>
    <button aria-expanded="false" aria-haspopup="true" aria-label="More options" class="c-action-trigger" data-bi-id="msgEllipsisBtn"> </button>
     

    Question Info


    Last updated November 16, 2019 Views 2 Applies to:
    Saturday, November 16, 2019 6:07 PM

All replies

  • Hi,

    Thanks for your question.

    1. Have you tried to run your exe file manually? Does it create the text file to "c:\users\me\desktop" path?

    2. Also, which user you setted to run the task?

    You need to set the user account with enough permissions to read and execute your exe file in  "C:\users\me\desktop" and write file in this path.

    3. When Run with highest privileges and Run whether user is logged on or not is checked, your program will run in the background without any GUI. Please try to check your task manager whether the program is running.

    Best regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, November 18, 2019 2:17 AM
    Moderator
  • The program probably writes to the current directory. (Look for the file in c:\windows\system32.)  

    You need to set that in the task definition.

     
    Monday, November 18, 2019 3:26 PM
  • Thanks for your reply but I have already searched my computer in case the file is somewhere else and also tried with the destination in the start in box.

    Stephen

    Monday, November 18, 2019 3:46 PM
  • Is this a program that you wrote or is it something you downloaded or got installed as part of a software package?

    Is it a command line program or a GUI (Windows forms) program? In Task Manager (manager, not scheduler) do you see any instances of your program executing? 

    If it's a command line program, then you can trace stdout+stderr by creating a c:\users\me\desktop\myprogram.bat file like this.

    C:
    cd c:\users\me\desktop
    myprogram.exe

    In the task definition have it execute cmd.exe and in the parameters use:

    /c c:\users\me\desktop\myprogram.bat 1> c:\users\me\desktop\myprogram.log 2>&1 

    If that doesn't help you figure out the problem, then run process monitor and set it to filter on myprogram.exe. See if it tries to create the file and if you get an access denied somewhere.  

    Monday, November 18, 2019 4:27 PM
  • Written myself in VB.Net
    GUI Windows Form.
    In Task Manager I can see it when run manually and it creates the text file.
    Cannot see it when run from Task Scheduler and there are no entries in Process Monitor

    Stephen

     

    Monday, November 18, 2019 11:45 PM

  • Cannot see it when run from Task Scheduler and there are no entries in Process Monitor


    Check the task history to see if it launched the program and any status codes that it returned. 

     

    Make sure that you are filtering on process name. Like mmc.exe here.

    If you still don't see it, configure the task to run cmd.exe which runs the bat file. Then remove the procmon filter that you added, and look in all entries for cmd.exe and the calls that it makes. Is the log file created? (Might be empty since you have a GUI process.) But if the file gets created then you know the that the task ran.

     

    Written myself in VB.Net
     

    Add in try/catch for the main processing code. In the catch section write the exception details to the application event log. Maybe also write "Myprogram starting." and "Myprogram ending" to the event log.  That way you know that you are being launched. 

    You might also want to convert it to a command line program so that you don't get an inadvertent message box that no one can see. 

    Tuesday, November 19, 2019 2:34 AM
  • Hello sgell,

    You can also enable file access auditing on the C:\users\me\desktop folder and see whether your .exe file tries to write anything to it. You should first look at the event 4663 in the Security log - https://www.ultimatewindowssecurity.com/securitylog/encyclopedia/event.aspx?eventID=4663 - it will show you if there were attempts to access the me folder at all and if they really were the 4656 events (Audit Failure) may be present in the Security log. In both events pay attention to the Accesses field which displays the access type requested by your application.


    Regards,
    Michael



    • Edited by MF47 Tuesday, November 19, 2019 7:35 AM
    Tuesday, November 19, 2019 7:32 AM
  • Here are the Process Monitor results:

    It seems to me that although the program runs manually it does not run from Task Scheduler.


    Wednesday, November 20, 2019 6:08 PM
  • Setup.exe? That's the name of your program? Or does your program do some installation the first time that a user runs it?

    Have you logged on to the desktop with the account that the task is configured to execute as and successfully ran your program?  


    • Edited by MotoX80 Wednesday, November 20, 2019 8:52 PM
    Wednesday, November 20, 2019 8:30 PM
  • Setup.exe is the name of the program.
    Does your program do some installation the first time that a user runs it? No
    Have you logged on to the desktop with the account that the task is configured to execute as and successfully ran your program?  Yes

    Here is the relevant code in vb.net:

            Dim ReportFile As System.IO.StreamWriter
            ReportFile = My.Computer.FileSystem.OpenTextFileWriter("c:\users\balfour\desktop\Report.txt", False)

            Line = "Daily Checks and Stock Update at " + Format(Now, "dd/MM/yy")
            Line = Line + vbNewLine + Format(Now, "HH:mm:ss") + " Started"

            'update and checking routines ------------------------------------------
            Indexing()
            FreeRecords()
            FreeItemLocks()
            ComponentColours()
            UpdateStocks()
            '-----------------------------------------------------------------------

            Line = Line + vbNewLine + Format(Now, "HH:mm:ss") + " Finished"
            ReportFile.WriteLine(Line)
            ReportFile.Close()

            End


    Wednesday, November 20, 2019 10:43 PM
  • There should be more than 6 process monitor events for your program. 
    • Edited by MotoX80 Thursday, November 21, 2019 2:16 AM
    Thursday, November 21, 2019 1:13 AM
  • Drag an eventlog icon on to your form. It should default to name eventlog1. Put a try/catch around your main code section. You will need to run VS or the app as an admin the first time to get the event source registered. 

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            EventLog1.Log = "Application"
            EventLog1.Source = "MyProgram"
            If Not EventLog.SourceExists("MyProgram") Then
                EventLog.CreateEventSource("MyProgram", "Application")
            End If
            EventLog1.WriteEntry("MyProgram starting")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim zero As Integer = 0
            Dim mybad As Integer = 1
            Try
                mybad = mybad / zero
            Catch ex As Exception
                EventLog1.WriteEntry("MyProgram crashed: " & ex.Message)
            End Try
        End Sub
    
        Private Sub Form1_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
            EventLog1.WriteEntry("MyProgram ending")
        End Sub
    End Class
    

    Then whenever you program runs, there will be events created in the application eventlog. If something crashes, that will be logged.

    Thursday, November 21, 2019 2:26 AM
  • Hi

    Added Event Log code as above.
    Program runs OK manually.
    From Task Scheduler there are no entries in the event log.

    Regards

    Stephen

    Thursday, November 21, 2019 10:19 AM
  • Configure the task to call the bat file and modify it to return the errorlevel from your program. Maybe that will tell us something. It will show up in the log. 

    C:
    cd c:\users\me\desktop
    setup.exe
    echo %errorlevel%

    Otherwise, you're going to have to go back to the procmon trace and look at all of the entries around the time that the task ran.  

    Thursday, November 21, 2019 2:34 PM
  • Modified BAT file as above.
    Ran manually without any error reported.
    Ran from Task Scheduler and Task Manager shows setup.exe*32 running but no output file created and no errors reported.

    Task Scheduler History shows:

    The program took 47 seconds to run. 

    Process Monitor show:

    Thursday, November 21, 2019 3:39 PM
  • Can you share the entire procmon trace so that I can look at it? 
    Thursday, November 21, 2019 4:05 PM
  • Saved the procmon trace as a zip file.

    How can I add it to this reply? 

    Thursday, November 21, 2019 4:31 PM
  • You can't. You can put it on my OneDrive here. 

    I'll remove that link from the post after I download the file. 

     
    • Edited by MotoX80 Sunday, November 24, 2019 5:17 PM
    Thursday, November 21, 2019 6:53 PM
  • Done
    Thursday, November 21, 2019 7:17 PM
  • That's just the utility itself. I need the .pml log file.

    Thursday, November 21, 2019 8:31 PM
  • Hope I got it right this time ...
    Friday, November 22, 2019 12:14 AM
  • Got it. 

    So I found some interesting things. Not exactly sure what they mean, though.

    The first oddity, is that your start.bat is doing the following I/O. Is it just launching your setup.exe?

    What's in C:\Users\balfour\AppData\Local\Temp\VSD8D08.tmp\install.log?

    Your setup.exe keeps looking for {20FD4E26-8E0F-4F73-A0E0-F27B8C57BE6F}. On my Win10 Home machine I found that in Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Low Rights\ElevationPolicy\{1F1E561D-AF17-4510-B996-351BBA0862A7}\CLSID

    Are you automating IE? Is your task defined to "Run with highest privileges"? 

    What is a ClickOnce application? See https://www.mking.net/blog/programmatically-launching-clickonce-applications

    Your setup.exe pid is launching rundll32.exe's. 

    Friday, November 22, 2019 2:25 AM
  • This is the .bat file

    C:
    cd c:\users\balfour\desktop
    setup.exe
    echo %errorlevel%

    What's in C:\Users\balfour\AppData\Local\Temp\VSD8D08.tmp\install.log?

    The following properties have been set:
    Property: [AdminUser] = true {boolean}
    Property: [InstallMode] = HomeSite {string}
    Property: [NTProductType] = 3 {int}
    Property: [ProcessorArchitecture] = AMD64 {string}
    Property: [VersionNT] = 6.1.1 {version}
    Running checks for package 'Microsoft .NET Framework 4.5.2 (x86 and x64)', phase BuildList
    Reading value 'Release' of registry key 'HKLM\Software\Microsoft\NET Framework Setup\NDP\v4\Full'
    Read integer value 528049
    Setting value '528049 {int}' for property 'DotNet45Full_Release'
    Reading value 'v4' of registry key 'HKLM\SOFTWARE\Microsoft\NET Framework Setup\OS Integration'
    Unable to read registry value
    Not setting value for property 'DotNet45Full_OSIntegrated'
    The following properties have been set for package 'Microsoft .NET Framework 4.5.2 (x86 and x64)':
    Property: [DotNet45Full_Release] = 528049 {int}
    Running checks for command 'DotNetFX452\NDP452-KB2901907-x86-x64-AllOS-ENU.exe'
    Result of running operator 'ValueEqualTo' on property 'InstallMode' and value 'HomeSite': true
    Result of checks for command 'DotNetFX452\NDP452-KB2901907-x86-x64-AllOS-ENU.exe' is 'Bypass'
    Running checks for command 'DotNetFX452\NDP452-KB2901907-x86-x64-AllOS-ENU.exe'
    Result of running operator 'ValueEqualTo' on property 'InstallMode' and value 'HomeSite': true
    Result of checks for command 'DotNetFX452\NDP452-KB2901907-x86-x64-AllOS-ENU.exe' is 'Bypass'
    Running checks for command 'DotNetFX452\NDP452-KB2901954-Web.exe'
    Result of running operator 'ValueNotEqualTo' on property 'InstallMode' and value 'HomeSite': false
    Result of running operator 'ValueGreaterThanEqualTo' on property 'DotNet45Full_Release' and value '379893': true
    Result of checks for command 'DotNetFX452\NDP452-KB2901954-Web.exe' is 'Bypass'
    Running checks for command 'DotNetFX452\NDP452-KB2901954-Web.exe'
    Result of running operator 'ValueNotEqualTo' on property 'InstallMode' and value 'HomeSite': false
    Result of running operator 'ValueGreaterThanEqualTo' on property 'DotNet45Full_Release' and value '379893': true
    Result of checks for command 'DotNetFX452\NDP452-KB2901954-Web.exe' is 'Bypass'
    'Microsoft .NET Framework 4.5.2 (x86 and x64)' RunCheck result: No Install Needed
    Launching Application.
    Application appears to be an application manifest
    Launching application manifest via dfshim
    LaunchApplication failed with HRESULT '-2147024891'
    Running command 'c:\Users\balfour\Desktop\Daily.application' with arguments ''

    Your setup.exe keeps looking for {20FD4E26-8E0F-4F73-A0E0-F27B8C57BE6F}. On my Win10 Home machine I found that in Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Low Rights\ElevationPolicy\{1F1E561D-AF17-4510-B996-351BBA0862A7}\CLSID

    Are you automating IE? Not that I'm aware of.
    Is your task defined to "Run with highest privileges"? No

    What is a ClickOnce application? See https://www.mking.net/blog/programmatically-launching-clickonce-applications This describes the application created when I publish from within the programming environment.

    Your setup.exe pid is launching rundll32.exe's. 

    Friday, November 22, 2019 10:06 AM
  • Set the task to run only when the user is logged on. Then log on with that account and run the task. See what that does. 
    Friday, November 22, 2019 11:44 AM
  • In your Daily.application file, do you have a <deploymentProvider> section? What does it point to? A local drive, a mapped drive, UNC path, HTTP address?
    Friday, November 22, 2019 9:04 PM
  • Hi 

    Sorry for the delayed reply.
    One of the reasons the program was not running was due to errors caused by some data files being open. This is now fixed and it runs perfectly.

    From Task Scheduler with run whether user is logged on or not the program does not start with run with highest privileges checked or not checked.

    From Task Scheduler with run only when user is logged on the program does not start with run with highest privileges checked or not checked.

    In your Daily.application file, do you have a <deploymentProvider> section? What does it point to? A local drive, a mapped drive, UNC path, HTTP address?
    C:\Users\balfour\Desktop\

     

    Saturday, November 23, 2019 7:22 PM
  • So the program runs when you launch it, but it still will not run as a scheduled task? 
    Sunday, November 24, 2019 12:10 AM
  • So if I were the sysadmin responsible for supporting this server, and you were the app developer asking me to automate this program, I would request that you follow some basic requirements.

    - Do not use any technology that is designed to interact with a human. That include Windows forms that might display message boxes, Clickonce apps that might display an interactive dialog where "a user" is expected to click on a button.

    - Your program should be a command line program so that we can capture stdout/stderr. There should be sufficient logging code within the program to report on any error that it encounters. "Trying to connect to database, got error 8004005".

    - Any sub programs that are called should also be command line and installed on the server. You should not rely on any "install on the fly" technology.  Call the .exe directly.

    Yeah, you might eventually get your program running as a task. But the first time that it puts up a "click ok to continue" message box, you're done. You'll have no idea what is going on because there is no way for you to see that dialog. (In earlier Windows versions that were some "error message instrument" registry settings that would log message boxes. I tried without success to get those to work on win 10.)  

    You have to design your application for an unattended environment if that's how you want to run the program.  

    Sunday, November 24, 2019 1:10 AM
  • First I would like to thank you for spending so much time trying to help me.
    I am not a professional programmer but self taught over the last 40 years, starting with Basic Plus and now using vb.net. I only write for my own family hat manufacturing business which started in 1907 in the UK and is now based in China with a design studio, showroom and office in England.

    This program has one Startup Form but there are no controls so there is no human interaction.

    I have created a zip file of the code which is only 487kb, would you like me to send it to you?

    By the way where are you based.

    Regards
    Stephen

        

      
    Sunday, November 24, 2019 12:08 PM
  • If there are no controls, then it should be relatively easy to create a new command line project and copy and paste the code into it.

    No thanks, I don't need to see your code. 

    I am in Harrisburg Pennsylvania. Click on my user name to see my bio.  Some people like to do crossword puzzles to keep their brains active in retirement. I like to see if I can figure out user's problems on this site. Yeah, that my idea of "fun". I think I landed on my head too many times racing bikes.  

    Sunday, November 24, 2019 3:26 PM
  • You can try adding these registry entries to capture msgbox's. It works on 2008R2. 

    reg add  "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Error Message Instrument" /f 
    reg add  "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Error Message Instrument" /v EnableLogging /t  REG_DWORD  /d  1 /f
    reg add  "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Error Message Instrument" /v LogSeverity /t  REG_DWORD  /d  0 /f
    reg add  "HKLM\System\CurrentControlSet\Services\EventLog\Application\Error Instrument"  /f
    reg add  "HKLM\System\CurrentControlSet\Services\EventLog\Application\Error Instrument"  /v TypesSupported /t  REG_DWORD /d  7 /f
    reg add  "HKLM\System\CurrentControlSet\Services\EventLog\Application\Error Instrument"  /v EventMessageFile /t  REG_EXPAND_SZ  /d  "%SystemRoot%\System32\User32.dll" /f
    

    Log Name:      Application
    Source:        Error Instrument
    Date:          11/24/2019 11:32:47 AM
    Event ID:      1072
    Task Category: None
    Level:         Information
    Keywords:      Classic
    User:          US915FP08\Admin
    Computer:      US915FP08
    Description:
    Error Instrument: ProcessName: wscript.exe  WindowTitle: unknown  MsgCaption: Windows Script Host  MsgText: foo  CallerModuleName: unknown  BaseAddr: 0XFFFFFFFFFFFFFFFF  ImageSize: 0XFFFFFFFF         ReturnAddr: 0X0000000000000001

    Sunday, November 24, 2019 4:43 PM
  • It should be relatively easy to create a new command line project and copy and paste the code into it.
    I have never created a command line project so I'll do a bit of research to see how easy it is.

    As per you previous advice I created an Application log and added several event messages.

    Private Sub StartUp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            EventLog1.Log = "Application"
            EventLog1.Source = "MyProgram"
            If Not EventLog.SourceExists("MyProgram") Then
                EventLog.CreateEventSource("MyProgram", "Application")
            End If
            EventLog1.WriteEntry("MyProgram starting")
    This the first message but when run with TS this doesn't appear in the log.

    Regards
    Stephen

     

    Monday, November 25, 2019 6:18 PM
  • Please excuse my delayed response.
    I found one or two unhandled errors in my program which was causing the program to crash, for example, when it tried to open a file for exclusive access and the file was already open.
    These have now been fixed.
    My company's IT support company also helped with Task Scheduler which now runs the program OK with Run whether user is logged on or not and Run with highest privileges checked.
    There remains one problem when running three of the routines within the program.
    They work through data files record by record from beginning to end. They perform some checks on each record sometimes making some changes.
    When run manually they work fine but when run from TS the program stops without reporting an error.
    I added Event Log entries to try and find what was happening. 
    EventLog1.WriteEntry("Stock Colour Record = " + Str(StockColRecord))
    These entries were logged as the program work through each file and the program was completed successfully.
    Any idea why the program runs manually without these log entries but only runs successfully if they are added?
    Regards
    Stephen
    Sunday, December 1, 2019 11:33 PM

  • Any idea why the program runs manually without these log entries but only runs successfully if they are added?

    Given the fact that you have earlier commented that you are "not a professional programmer" and that you now have found "one or 2 unhandled errors", then a logical assumption would be that you have more unhandled errors in your code. 

    If you are going to design a program to run unattended, there should never be a question of "what was I doing?" or "what error did I get?". Your code should have sufficient logging and error handling programmed in to it so that you know exactly what it was doing.  I did quick search on my C drive and found this log where I pulled out the first few lines. 

    Writing to the application eventlog is ok for a few entries, but if your program is doing a lot of processing, then a more robust logging solution would make more sense.    

    01/22/16 18:58:58:  Beginning package AnyConVPN 3.1.05178

    01/22/16 18:58:58: 
    01/22/16 18:58:58:  Package Template Version: 2009-04
    01/22/16 18:58:58:  Install Source = C:\Users\Dave\Documents\Cisco_Anyconnect_WIN_PKI_3_1_05182-1
    01/22/16 18:58:58:  Command Line =
    01/22/16 18:58:58: 
    01/22/16 18:58:58:  *************** Begin initializing variables and collecting information ***************
    01/22/16 18:58:58: 
    01/22/16 18:58:58:  Collecting OS and PC information
    01/22/16 18:58:58:  The OS Path is C:\WINDOWS
    01/22/16 18:58:58:  The OS version is 5.1
    01/22/16 18:58:58:  Product Name is Microsoft Windows XP
    01/22/16 18:58:58:  Checking to see if the SysWOW64 directory exist.
    01/22/16 18:58:58:  Setting OS_64Bit to YES
    01/22/16 18:58:58:  Operating System = Windows XP
    01/22/16 18:58:58:  Installed Service Pack level = 3
    01/22/16 18:58:58:  Operating System is a 64 Bit Operating System
    01/22/16 18:58:58:  Unsupported Operating System
    01/22/16 18:58:58: 
    01/22/16 18:58:58:  Setting status  for MIF to: Failed
    01/22/16 18:58:58:  Setting reason for MIF to: Unsupported OS
    01/22/16 18:58:58:  Executing EXIT section
    01/22/16 18:58:58:
    01/22/16 18:58:58:  Creating AnyConVPN3.1.05178.mif file with Status: Failed  and  Reason: Unsupported OS
    01/22/16 18:58:58:  Copying SMSInvy.exe to C:\APPS\TEMP\SmsPkgUtils
    01/22/16 18:58:58:  Start SMSInvy.exe HINV D C:\apps\logs\HINV.log
    01/22/16 18:59:00:  Package complete
     

    Monday, December 2, 2019 12:36 AM
  • I added Event Log entries to try and find what was happening. 
    EventLog1.WriteEntry("Stock Colour Record = " + Str(StockColRecord))
    These entries were logged as the program work through each file and the program was completed successfully.

    It occurred to me that, without that line, the program might be stopping because it could be running too fast.
    In place of the EventLog line I added -  
     System.Threading.Thread.Sleep(5)

    This seems to have fixed the problem

    Regards

    Thursday, December 5, 2019 11:19 AM
  •  because it could be running too fast.


    Well, if it's working, that's great. Running too fast, eh, not sure on that one. Did you convert all programs to command line? Or are you still using a form? If you have controls on the form you might need to give VB time to process events. System.Windows.Forms.Application.DoEvents()

    https://www.oreilly.com/library/view/vbnet-language-in/0596003080/re11.html

    Thursday, December 5, 2019 2:41 PM
  • The whole program is now a console one so there are no window forms or controls.

    It's now working so thanks for all your help.

    Stephen

    Monday, December 9, 2019 7:18 PM
  • Excellent. 
    Tuesday, December 10, 2019 2:28 PM