Why is my batch file not running to completion when called from a scheduled task on Win 10

pk_oz 186 Reputation points
2022-01-21T04:03:21.46+00:00

I created a batch file to write to log files in a network folder, which I would also use to flag the job has run once. The batch file completes when I run it directly in a cmd window, however when I try running this from a scheduled task, which has been setup to use the system account, it only ever writes 1000 to the file. Why isn't my file running to completion? I added the call before the echo commands, tried () & (), tried calling batch files from a master and always half runs the batch job. The second "call echo" never runs.

@Echo off
set filename=%COMPUTERNAME%.txt
REM set testfile=C:\Scripts\pickup\%filename%
set testfile=\fltmeladc02\Tracelogs\Datto\%filename%
IF NOT EXIST %testfile% (
call echo 1000>%testfile%
REM Set-Service -Name "CylanceSvc" -StartupType disabled
call echo 1100>%testfile%
REM shutdown /r
)

The test workstations are Win10
The task is Win 10, run with highest permission

Windows for business Windows Client for IT Pros Networking Network connectivity and file sharing
{count} votes

Accepted answer
  1. Limitless Technology 39,916 Reputation points
    2022-01-25T17:17:56.24+00:00

    Hello

    Thank you for your question and reaching out.

    I can understand you are facing issue with running script using Task scheduler.
    I can see you you have used set-service PowerShell command in your batch file due to which it may not be ruuning

    You can replace it with equivalent batch command using "sc config" as below to disable the service.

    sc config "CylanceSvc" start=disabled

    Also remove REM from start of your line to see if there is any error or warning being logged So you final script should like as below.

    @Echo off
    set filename=%COMPUTERNAME%.txt
    set testfile=C:\Scripts\pickup\%filename%
    set testfile=\fltmeladc02\Tracelogs\Datto\%filename%

    IF NOT EXIST %testfile% (
    call echo 1000>%testfile%
    sc config "CylanceSvc" start=disabled
    call echo 1100>%testfile%
    shutdown /f /r
    )


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2022-01-25T20:10:19.77+00:00

    when I try running this from a scheduled task, which has been setup to use the system account, it only ever writes 1000 to the file

    Since the task is running as SYSTEM, you need to grant access to the Active Directory computer account for the Win10 PC in order to access the share.

    That account is:

    YourDomainName\TheWin10PCName$
    

    You would need to verify that the account has permissions at both the share and folder levels.

    Typically one would use some domain account to write to a share. When using SYSTEM, it's better to just write everything to the C:\ drive.

    In general, I recommend configuring scheduled tasks to capture program output so that you have a log file to examine when something doesn't work. Change the scheduled task to execute program "cmd.exe".

    In the arguments field set it like this example:

    /c C:\Scripts\MyScript.bat  1>>"C:\Scripts\Logs\MyScript-%date:~10,4%-%date:~4,2%%date:~7,2%.log" 2>&1
    

    That will create a daily log file of all executions of the task. That will capture stdout and stderr for programs that get called.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.