[SOLVED] Windows 10: schtasks job start, but does not work

erotavlas

New Member
Hi,
I'm new of this thread and I do not know if this is the right section of the forum. I'm under windows 10 1803 64 bit.
I have two scripts (.cmd and .ps1) which execute the same operation. The scripts work well if I run them from cmd or powershell while they do not work if I use schtasks.
In particular, the job is created correctly and it starts after login, but the scripts fail to execute. The script executes a program in a new windows, but it is closed immediately and it is not possible to catch the error.
I tried to redirect the output of cmd in order to capture the error, but it does not work.
Code:
start cmd /c program arg1 arg2 2^> out_err.txt
Do you have any suggestion?
 
Last edited:

Neemobeer

Windows Forum Team
Staff member
Did you try adding the word PAUSE or change it to cmd /K. /C will terminate the command prompt.
 

erotavlas

New Member
Thank you very much. Your workaround worked, by using cmd /k I found that the problem was related to the working directory.
Both the following works well.
Code:
start /D %scriptPath% %scritpPath%program arg1 arg2
Code:
start /D %scriptPath% cmd /c %scriptPath%program arg1 arg2
I would like to get the same result via powershell:
Code:
Start-Process -WorkingDirectory $path -FilePath "$path\program" -ArgumentList "arg1 arg2" -WindowStyle Maximized
However, in this case adding the path does not change the effect.
 

Neemobeer

Windows Forum Team
Staff member
Use the argument field available in task scheduler, also do you have your execution policy set to allow scripts?
 

erotavlas

New Member
I solved in this way:
Code:
Start-Process -WorkingDirectory $path -FilePath "$path\program" -ArgumentList "arg1 arg2" -WindowStyle Maximize
Schtsk:
Code:
PowerShell -ExecutionPolicy Bypass -NoProfile -InputFormat None -File C:\path\script.ps1
 
Top