How to execute cscript using windows task scheduler?
Asked Answered
B

3

4

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?

Blakey answered 27/9, 2012 at 18:56 Comment(6)
You do not specify paths to your script (only script name). I've seen this causing problems with Task Scheduler (even when the script is in the supposed/set working directory)Michaelemichaelina
Used path for everything and still the same result: C:\Windows\System32\cscript.exe c:\tableaudata\CSV_To_Excel.vbs c:\tableaudata\test.csv c:\tableaudata\test.xlsxBlakey
Are you 100% sure it executes? Do you see something similar to: Result: The task completed with an exit code of (0) in log? Have you deleted/renamed the log prior to testing?Michaelemichaelina
Yes, 100% sure it executes. I delete log.txt before each run and it gets recreated with the message "file converted". I also tried dumping the cscript line to output.txt. That also gets created. Here is the content in output.txt: Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved.Blakey
Maybe this would help: #8926408. You could also add win version and if you run it under your account or other one so hopefully you'll get better answers. Sorry I couldn't be of more help...Michaelemichaelina
Tried all 4 ways from that post and the same result. Hey, THANKS for helping!!Blakey
B
4

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

Blakey answered 18/10, 2012 at 16:52 Comment(1)
Exactly the same problem. Solved by running as a user with admin privileges AND selecting a checkbox called "Run with highest privileges" before it would work.Ronen
C
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.

Canova answered 28/9, 2012 at 0:56 Comment(4)
MyCScriptOutput.txt has the following text in it: Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved. When I run with //NoLogo //B the file has no content. Again, no xlsx created.Blakey
If it helps, when I doubleclick the .bat file manually, the command window appears, stays for a bit (I'm guessing for the duration of the conversion proess), then disappears. When the task scheduler executes, the command window doesn't appear. I just see log.txt created.Blakey
It's hard to debug CSV_To_Excel.vbs without seeing it, but does it import any other units or make any file references that might depend on the working directory? What happens if you make all of the cscript line file references absolute paths and add the line [START OF LINE] cd /d "%~dp0" [END OF LINE] before your cscript line?Canova
Here are the files I'm using: http://dropcanvas.com/5ckz9 Adding that line to .bat didn't yield the expected result (I'm already using all absolute paths in .bat file)Blakey
D
0

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

Defy answered 5/4, 2019 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.