How to execute cscript using windows task schedule

2019-07-05 13:07发布

问题:

The problem: When I double click the .bat file it executes as expected. When I schedule it in Windows Task Scheduler it executes except the line that has cscript.

Content of .bat file:

@echo off
cls

cscript CSV_To_Excel.vbs c:\tableaudata\test.csv c:\tableaudata\test.xlsx
echo.file converted >>log.txt

What is throwing me off is the fact that log.txt gets created indicating that the .bat file is being executed. But .xlsx is not created. However, on manually double clicking .bat both log.txt and test.xlsx is created.

What could be the problem?

回答1:

Resolved!! In the windows task scheduler I had to click "change user or group" button and add "Administrators" group.



回答2:

To help debug the situation, add the following to the end of your cscript command line:

>>c:\MyCScriptOutput.txt 2>&1

Then, check to see if the c:\MyCScriptOutput.txt file has any error message(s) in it. If it does, please add this information (both the command line and the output) to your question.

I'm speculating, but the problem might be that cscript is trying and failing to run interactively, so you could try replacing "cscript" in your command line with "cscript //Nologo //B", to see if that fixes it.



回答3:

The main problem is you don't specify full path to your CSV_To_Excel.vbs Scheduler execute script from c:\windows\system32 (where schtasks.exe located) So, your batch call to cscript should be

cscript %~dp0\CSV_To_Excel.vbs c:\tableaudata\test.csv c:\tableaudata\test.xlsx
echo.file converted >> %~dp0\log.txt