bat/cmd file do not work via task win scheduler - windows

I have job in task scheduler on windows local server admin account that runs bat/cmd file that execute python script as different user (with accesses to locations and database) to make calculations, prepare file and then execute dbo procedure.
And it is sad :(
Job have action 'start a program' (I tried running it with both .cmd and .bat):
C:\PythonScripts\<run_data_prepare>.bat
And if bat/cmd file only contains an easy procedure, then it work properly:
python C:\PythonScripts\<python_script>.py
But if file contains code below it will only execute properly if I open it manually. If I open it via task scheduler, then it will start task and action and complete it (from scheduler point of view) but even the first command is not executed.
C:\Windows\System32\runas.exe /user:<domain>\<ServiceUser> /savecreds "python C:\PythonScripts\<python_script>.py"

Related

sccm 2012 task sequence not running a exe or hta

I am trying to run a EXE or HTA file from command line, all it is, is a popup message that informs the end users of what is about to be deployed. I am run it from command line locally and it works great, but when I set it up as a task sequence it will not run.
Have you examined the log file smsts.log?
Run the HTA using a Run Command Line step.
Here is an example: %SYSTEMROOT%\system32\mshta.exe %ScriptRoot%\MyCustomHTA.hta
You could refer to the sample from here.
HTA in SCCM Task Sequence

VBS script does not work properly from Task Scheduler in Windows 10

I have a windows server designed to perform mail merges for postal letters.
A outside server generates xls data sheets and SFTPS them to my windows server, along with a JSON file with further instructions:
The Letter to merge with (docx file)
An E-mail address to send a PDF of the merged letters.
The script works like this:
1. Constantly(every 60 seconds) monitor directory for JSON files. ->
2. If there is a JSON file, parse it. ->
3. Find XLS file related to that JSON file ->
4. Run VBA/ EXCEL macro to mail merge XLS and the letter into a PDF ->
5. Email PDF to appropriate contact ->
6. Move XLS file to Processed Folder
7. Move JSON file to PRocessed Folder
If I run it manually from the command line it works just fine. However, If I run it from Task Scheduler, either with "RUN" or automatically when the machine restarts or when a given user logs in the script fails. Steps 1, 2, and 3 work. The VBA code/ Macro fails to work.
I'm hoping this can be resolved through configuration of Task Scheduler. Here is my current setup:
Script runs from a bat file, the message script keeps a CMD window open that explains what the script is doing:
cscript C:\automerge\message.vbs
cscript C:\automerge\automerge.vbs >> C:\automerge\logs.log
Task Scheduler Settings:
Run only when user is logged on
Run with Highest Privileges
Configure for Windows 10 (tried using 7 and 8 too)
Trigger:
Trigger at logon - Enabled
Action:
Start a program
Details:
"C:\AutoMerge\JSON Watch.bat"
Conditions:
Start the task only if computer is on AC power
Stop if computer switches to battery power
Settings:
Allow Task to run on demand
If the running task does not end when requested, force it to stop
Can anyone give me advice on how to change the scheduled task or batch file to get this to work?
Try executing like this instead of calling the batch file directly.
32-bit
Action: "%SYSTEMROOT%\system32\cmd.exe /c C:\AutoMerge\JSON Watch.bat"
or 64-bit
Action: "%SYSTEMROOT%\SysWOW64\cmd.exe /c C:\AutoMerge\JSON Watch.bat"
If it is 64-bit... update your cscript to execute %SYSTEMROOT%\SysWOW64\cscript.exe
Turns out Noodles was right it was something in the code.
I created a file system object and used it to get a File.Name. Then I passed this File.Name(without the path) to a function that parses the JSON in the file. This worked just fine when I manually ran it.
JSONExists(File.Name)
In order to run properly from Task Scheduler I had to make sure it passed the entire file name including the path.
FPath = "C:\Automerge\"
JSONExists(FPath & File.Name)
I'm not exactly sure why I need the whole path with Task Scheduler and not when I run manually, but at least now I have a good rule of thumb.

Windows TaskScheduler - Run locally an executable from a remote server in a batch file

I have a batch on my C:\TEMP\batch.bat who runs an exe from a remote server (\Remote_host\App_Folder\app.exe)
My .bat content:
start /d "\\Remote_host\App_Folder\" app.exe
On double click, the batch file runs app.exe correctly. So now, I create a task in Windows Task Scheduler which triggers this batch file.
When task is launched, app.exe is not called.
Any suggestions ?
The task has to run with a specific account and make sure the checkbox "Run whether user is logged on or not." is not checked.
If checked, it will not store the password.

SVN update using windows task scheduler not working

I want to setup a windows cron job to update svn. I created a batch file with this step:
START TortoiseProc.exe /command:update /path:"C:\svn" /closeonend:0
and setup the task scheduler to run this daily. This svn requires a password which I want to enter each time rather than cache it. The batch file works as expected without any issues.
The task scheduler shows that the job was successfully executed. However, I don't get the GUI to enter the password, neither does the directory get updated. Also, when I tried to manually update the svn directory thereafter, it says that svn is locked.
Why is task scheduler reporting successful?
Why is svn getting locked?
Task scheduler reports success because START seems to return exit code 0. START only starts an application and exits. It doesn't wait until the started application has exited. The task scheduler never sees the exit code of TortoiseProc this way.
You can do this with START WAIT. Enter START /? in a CMD for more information.
You can also do this with CMD /K itself. Enter CMD /? in a CMD for more information.
But I don't think you need START or CMD at all. You can either run TortoiseProc directly or call it via a batch script. In the batch script you can add more debugging like echos and redirected output in files to see what happens.
Do not use TortoiseProc.exe for this task! Use Subversion command-line client (svn.exe). Read the docs:
Remember that TortoiseSVN is a GUI client, and this automation guide
shows you how to make the TortoiseSVN dialogs appear to collect user
input. If you want to write a script which requires no input, you
should use the official Subversion command line client instead.

Task Scheduler fails to run batch file despite reporting task completed

I have a simple batch file which calls a powershell script.
I've looked at the following 3 previous questions on the subject as well:
Run a batch file from Task Scheduler is not working with a java command
Windows Task Scheduler doesn't start batch file task
Task Scheduler not executing batch (bat) file with MSTest commands
It seems like I've tried every single combination of running the task and it still doesn't execute my powershell script.
batch file contents:
powershell.exe "E:\SIS\fileCopy.ps1"
If I run the command in the batch file manually, it runs just fine. Here are things I've tried to do to get it working:
I've change the Security options to run as System with "Run with highest priveleges" checked
I've tried every other combination of "Run only when user is logged on", "Run whether user is logged on or not" and "Run with highest priveleges"
I've tried adding the "Start in (optional" setting to the folder where the files are located.
I'm at my wits end and can't believe Microsoft hasn't figured out a way to make this easier.
You need to have task scheduler execute Powershell.exe and have the arguments be the path to your .ps1 file.
To validate your script is running properly, you should set the Security options to 'Run only when user is logged on'. It will then pop the powershell dialog when it runs. I often also use start-transcript to view the results of scheduled poweshell scripts.
After you validate the script is running correctly, you can set the security options however best fits your situation.
Use the execution policy flag to flag that instance to unrestrisicted because your powershell settings may be blocking script execution.
powershell -executionpolicy unrestricted -Command "E:\SIS\fileCopy.ps1"
I found that Task Scheduler can't run a batch file if it lives in a folder that is being synced by OneDrive. I had to move the batch file to another folder to get Task Scheduler to be able to run it.

Resources