Can run an .exe successfully from command line, but not from Task Scheduler - scheduled-tasks

I am able to run a .exe from the command line, but when I try and run it using the Task Scheduler, I get the error "The system cannot find the path specified. (0x80070003)"
I am running this on a server, so I have tried mapping the drive and also using the full path. Both of these methods work using the command line.
This is how I have the Program/script set to run:
D:\scripts\lilt\NewFile.exe \err00\root\LILT\ILL\ \pcc02\Inter\I040\ILL\Inbox\"
What do I need to do, to get this to run on the scheduler? Thanks!

I figured it out. I had to use the "Add arguments (optional)" section to indicate the drives and not put it in the Action line. (As some history, I'm moving this task from a PC to a server, and the PC had it all in the one line, as well, when running from cmd on the server, I could execute it successfully on one line, but it behaves differently once you make it a task apparently.
So this went in "Action":
D:\scripts\lilt\NewFile.exe
And this went in Add arguments (optional):
\err00\root\LILT\ILL \pcc02\Inter\I040\ILL\Inbox

Related

Silent bat file execute powershell command

We have an application server running as a service, when some configuration is loaded it starts a bat script which has to run the powershell command Stop-ClusterGroup DRMSERVICES and then start it again.
The bat file works flawless when I manually execute it by dobbelt clicking. But when the service is running the bat, it does not finish, or execute the powershell command.
Bat file looks as follows
#echo off
powershell -command Stop-ClusterGroup DRMSERVICES
powershell -command Start-ClusterGroup DRMSERVICES
The service runs the bat file in silent mode, as a main difference.
I have tried with various switches including the -ExecutionPolicy Unrestricted and START /wait etc
Creating a seperate ps1 file and have the bat execute this instead.
All with the same output:
Manually executing the bat works
When the service executes the bat, it does not work.
I know the bat file is executed by the service, as inserting NET STOP servicename is working correct.
In the powershell event viewer I can also see event of the powershell commands take place.
The difference between manually executing and have the service execute the command in the event viewer, is event id 800 which states info about 'execution pipe' this is not present when the service is executing the bat.
The service does not wait for the powershell, and thus it does not have time to stop the cluster before exiting.
I'm lost whether this is a permission issue, syntax error or whatever.
Hopefully somebody can help
UPDATE:
I have tried with all proposed solutions, all with same result, the bat file works when double clicked, but the service does not execute the powershell command. Pure cmd is executed, as I can pipe to a txt file. I even got to a point when trying runas that the output log text wrote "insert administrator password"
I even managed to have our software guy change our software to call a powershell directly instead of a bat, same result. Powershell won't execute the command, this tells me it probably is permission, but everything have been set to log in as admin and run as admin for the sake of success, but still nothing.
I solved the problem.
Because the service is a 32bit process, it will execute a 32bit powershell.
FailoverClusters module only exists as a 64bit module.
By using %SystemRoot%\sysnative\WindowsPowershell\v1.0\powershell.exe
The service is able to open a 64bit session, and thus use the failover cluster module.
As a side note, the sysnative folder is only visible from a 32bit session, therefore it cannot be found via browsing in a 64bit os.
I think i have dealt with this kind of issue before, after the,
powershell -command Stop-ClusterGroup DRMSERVICES
you need to have cmd wait for a certain number of seconds, and then test if the DRMSERVICES is now stopped, if it is stopped then to start the DRMSERVICES again. This way cmd will keep waiting, and then check if the service has stopped.
After a certain number of tries, maybe have a way to stop checking and exit the script, for example it is trying to stop the service, and has run into a problem.
There is a timeout command in cmd

VSCode task in WSL environment, terminal keeps exiting when trying to run shell script

NOTE
I've had to remove like two chunks of this post because stack overflow kept interpreting it as code when it isn't and it wouldn't let me post, I'll just make a screenshot of what the post is supposed to look like and post it here. Read this instead.
Summary
I was finally trying to learn how to use VSCode tasks and so I copied the first task example from here and created a shell script at scripts/test.sh which contains simply "echo foo". I also commented out the windows alternative script because I exclusively use WSL/Bash. Whenever I run the task I receive a "The terminal process terminated with exit code: 1" error message, which is no help whatsoever.
Testing
I ran various tests and I have no idea why this isn't working.
Proving The Task Is In The Correct Directory & In WSL
First I thought maybe the task isn't running in WSL or that the directories are out of sync, so I changed the commands to see what happens.
First, I changed it to:
"command": "pwd",
and the output was "/mnt/f/.../.../tmp/tmp.1BitOIA78E" (... are for some arbitrary path) so clearly I concluded I was running on WSL and in the right path.
Proving the Script is Executeable
Next I thought, maybe the script I'm trying to run isn't executable or something to that affect, so I changed the command to:
"command": "stat ./scripts/test.sh",
and I got the following output which shows, the file exists, it's executeable & can be accessed through "./scripts/test.sh" from whatever directory the Task is set to on construction
I run the task by typing ctrl-shift-P to open the menu, select "run tasks" and then select 'My First Task'.
Note: I don't think this is a settings problem. There're no workspace settings setup (because this is just me testing) & just in case, you can find my current user settings here which I updated immediately before posting this.
Expectations
What I'd like is either:
Someone to tell me how I can access the stderr and stdout log of the shell upon startup so I can get some actually helpful information as to why this is happening.
Someone to tell me why I can run a script perfectly fine outside of a task, but within a task it completely fails.
Whats also of note is that the script isn't the problem here. Leaving it completely blank, doesn't stop the terminal from straight up crashing.

Run a script to load commands into my main script

I have a powershell file that I have downloaded from ScriptCenter that allows me to control and query virtual desktops on my machine (https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5).
Using their example, I can run that ps1 file at the start of my script to use those commands that the script creates. All fine here.
The only issue with this is that when I run my script, it asks to confirm to run it. This is something I don't want my script to do.
To work around this, I tried using the "PowerShell" command with "-ExecutionPolicy Bypass" set. This removes the prompt to approve the script, however it stops the script from being loaded into my scripts session as I can't use any of the commands it make available by running it.
How do I either run the script first, without it prompting, or execute the powershell command so that it is run in the session space of my script so that its commands are available?
Thanks

Azure startup task, wait for all other task to finish

I have a startup task for my webrole that download some executable file from a blob and then proceed to the installation.
From a .cmd file, I start a power shell script that download the files, then I start the file from the .cmd.
The script works fine if I run it manually through RDP after the publishing is done.
But, when running as startup script, it sometimes (often) fail at different points.
The taskType is set to background.
Last time, the error was that the command PowerShell does not exists...
Also, I use powershell -command set-executionpolicy unrestricted before running my PS script, but I read here that other task may reset this setting and make mine fail.
Quite a mess.
So that makes me think that if I could wait for all other task to perform before starting mine, it would eliminate these kinds of problems
I suppose I could check if some process is running and wait for it to finish, but I have no clue wich process to check.
Or maybe there's another solution.
~edit~
I read here that the error about powershell not existing may be caused by the batch file being saved as UTF-8 in visual studio. I re-writed it from scratch in notepad++ and made sure it is save as ANSI. Then, same error. The full message is :
'PowerShell' is not recognized as an internal or external command,
operable program or batch file.
Again, the script run perfectly from command line in remote desktop.
It would be possible to set an environment variable at the end of the script that is required to finish, then in the script which is awaiting the dependencies, loop until the environment variable is set, then kick off its activities.
You could also run everything from a single powershell script and use the '-asjob' switch on your installer statement, use the 'wait-job' cmdlet to block until the task is complete then carry on. Powershell also offers a '?!' operator which ensures the last statement executed properly.
This might be caused by an encoding issue. As mentioned in this answer you should save your file in ASCII to ensure correct interpretation of your script.
From the linked answer:
Open your whatever.cmd file with your VS 2012 Ultimate. Click on File->Save whatever.cmd as -> on the dialog there is little arrow next to the [save] button. It will show up a menu that will have the option Save with Encoding.
Select it. Now choose "US-ASCII Codepage 20127" from the list of available encodings.

How do I get Powershell to execute in scheduled task?

I have created a Powershell script and set it up to run within a .bat file. When I click on the .bat file the Powershell script runs like a champ. I then set up the .bat file to be run as a scheduled task. Again it works like a champ when I right click on it and launch it interactively. Problem is it fails when the task scheduled to run unattended in the evening.
Not sure if this matters but I am using Powershell v.1 on Windows 2003 Server 64.
Thanks in advance for your help.
==========================================
The status on the scheduled task is "Could not start". Perhaps fails was the wrong choice of words because the task probably doesn't even start running.
The task is set up to run as my user and I am set up as a local administrator on the box.
Check if scheduled task's security setting
Scheduled task might be running under a credential that does not have access to the batch file.
When you are adding a new task, you are asked to enter a credential
And you can check for already created task's credential
[UPDATE]
The status on the scheduled task is "Could not start". Perhaps fails was the wrong choice of words because the task probably doesn't even start running.
Make sure that Task Scheduler service is running
Is the task set to run with your useraccount or as System? Check the access rights on cmd.exe and powershell.exe.
Also if you go to the sheduled tasks you can do a 'view log' in the advanced menu to check for error messages.
Just a guess, but could it be something to do with the account/security context from which the script is running? If it's running on the System account, then that could certainly be a problem. (I suspect it's just running on your user account however.) Check that all your path variables are set correctly when the script runs, for a start. Otherwise, could you perhaps provide any more information on how exactly it fails (i.e. any error messages)?
Maybe the current directory is not set as you expect when running from the task manager. Try running your bat file from the command line, while you are not in the same directory as the file (and your .ps1 script) are.
Otherwise: check permissions.