How to execute cscript using windows task schedule

2019-07-05 13:00发布

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?

3条回答
冷血范
2楼-- · 2019-07-05 13:22

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楼-- · 2019-07-05 13:25

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

查看更多
看我几分像从前
4楼-- · 2019-07-05 13:36

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

查看更多
登录 后发表回答