Create a task with task Scheduler to run cmd.exe with commands - scheduled-tasks

I created a task with the task scheduler in windows 10 to open cmd.exe. it ran successfully like this
But I need to run some commands every time it opens something like this
and automatically runs this command.
this is important that it opens a command prompt first and then runs the command inside the command prompt automatically.
Thanks.

Use /k flag. See cmd.exe /k switch.
If you don't need console window to stay after commands completed use /c flag. You can read more about them with cmd /?

Related

Run bash script in WSL through powershell script

I am trying to run a script which launches a WSL (ubuntu1804) terminal, and then run a bash script in that terminal
.\ubuntu1804.exe;
cd test_directory;
node server.js;
However after the first command the terminal open however, the two other commands aren't executed
.\ubuntu1804.exe by itself opens an interactive shell which PowerShell executes synchronously.
That is, until you submit exit in that interactive shell to terminate it, control won't be returned to PowerShell, so the subsequent commands - cd test_directory and note server.js - are not only not sent to .\ubuntu1804.exe, as you intended, but are then run by PowerShell.
Instead, you must pass the commands to run to .\ubuntu1804.exe via the run sub-command:
.\ubuntu1804.exe run 'cd test_directory; node server.js'
Note: Once node exits, control will be returned to PowerShell.

AutoHotKey run command in background

I'm trying to run a command with AutoHotKey that I would normally run with cmd.exe. Here is the command:
pandoc -s "C:\input.txt" -o "D:\output.html"
This is how I do it in AutoHotKey:
#a::
run pandoc -s "C:\input.txt" -o "D:\output.html"
return
The only problem is that this opens up the a command prompt called "pandoc". Normally I'd just type in the command in cmd.exe and it would run without any hiccups or any windows opening. For this, however, that pandoc window shows up. Am I doing it correctly? Is there any easy way to suppress the window and run the command in the background?
Runs a program without opening a window. The program is "cmd.exe", the windows command shell. It is invoked with arguments "/c time /t", which outputs the current time. It redirects the output to "c:\t.txt"
program
#a::
run cmd /c time /t > c:\t.txt, c:\, hide
return
output
c:\>type c:\t.txt
14:28

keep command line window open when running scheduled task executing batch file

I would like to have the console window kept open when running a scheduled task which executes a batch file. If I run it manually meaning execute the batch file the window stays open but through task scheduler it doesn't open but I can see the task is still running. I put a pause at the end to do this.
#echo off
TITLE PROCESS_MGR
tasklist /FI "IMAGENAME eq JOESMO.exe" | find /I "JOESMO.exe">nul &&(echo PROCESS
JOESMO.exe IS ALREADY RUNNING!
echo %DATE%
echo %TIME%
pause
) || (
echo JOESMO PROCESS IS NOT RUNNING
cmd /c start "JOESMO.exe" "C:\Users\xxxx\Documents\
Visual Studio 2010\Projects\Projects2013\JOESMO.exe"
pause)
I found this suggestion cmd /k myscript.bat but having creating the task in task scheduler for windows server 2008 I am not sure where to apply this. I added /k to the add arguments box in edit action in task.
In the Scheduled Task dialog, just before the name of the batch file it's going to run (it's labeled Program/script. You now have something like:
myscript.bat
Change it to
cmd
Add the following to the **Add Arguments (optional) entry:
/k "C:\My Batch File Folder\MyScript.bat"
Tested on my system (Win7 64-bit), and it worked perfectly. I'm looking at the open command window it created as I type this text. :-)
Unfortunately Ken's solution didn't work for me on a Windows 2008 R2 Std server, I was able to launch an interactive window by modifying the scheduled tasks setting using schtasks.exe
In a command window I did the following command:
schtasks /Change /TN "My Task" /IT
However that does require you be logged in as the same user context in which the scheduled task is executing. So if your scheduled task is use the localsystem "taskaccount" then you will have to log into the system as the "taskaccount" user.
Oddly enough it worked when I manually run the task but it didn't pop for me when it kicked off at a scheduled time.
Ken's answer didn't worked for me.
Found this way of doing :
in your BAT file (create one if you only have an EXE) :
start C:/Absolute/Path/To/MyScript.exe myScriptArg
works like a charm !
Note: In the scheduled task, you must check "Exec only if user is logged"
Create a shortcut to the Batchfile and put that in the action. Worked for me
I tried all of the above, but they did not work for me. Here is what I did to get this to work:
Platform
Windows Server 2003 R2 SP2
ActivePERL v5.10.1
Steps
Create DOS BATCH script -- this runs the actual program, ie, myscript.bat
Create PERL script to call the DOS batch script, ie, myscript.pl
myscript.pl is a 1-line script: system("e:\scripts\myscript.bat");
Create scheduled task: perl myscript.pl
The DOS command prompt window now always opens up. And more importantly, the task now successfully runs and completes. NOTE: The scheduled task RunAs user is logged in to the server.

From command line on win7, how do I launch a process as administrator with elevated UAC

I have a program which requires Administrative privileges that I want to run from a batch file. What command can I run from command line will run my program with administrative privileges? I'm okay with the pop-up window asking for permission. Additionally, the file needs to be able to run from anywhere on a computer so additional files are run from ./src. The problem is that if I right-click and choose "run as administrator" it changes my current directory so ./src no longer works. If I disable UAC on my machine then everything runs fine. Thank you!
Look here: https://superuser.com/a/269750/139371
elevate seems to be working, calling
C:\Utils\bin.x86-64\elevate.exe -k dir
executes dir in the "current directory" where elevate was called.
This is tough, Microsoft provides no utility to do this (mostly because giving a batch file that ability breaks security), except for RunAs, and that requires that the Administrator account be activated.
There IS a JScript program that can do something similar, by using SendKeys to open the Start menu and type cmd[CTL]+[SHIFT]+[ENTER] which will launch a Command-Line shell.
Save the following as as .js file, like StartAdmin.js:
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys("^{esc}cmd^+{ENTER}"); The equivilent of [CTRL]+[ESC] cmd [CTRL]+[SHIFT]+[ENTER]
To run StartAdmin.js from a batch file, you will need the following line:
wscript StartAdmin.js
To launch from a particular directory and launch a batch file, change line 2 in StartAdmin.js to something like:
WshShell.SendKeys("^{esc}cmd /C "cd %userprofile% & batchfile.bat"^+{ENTER}");
/C switch tells it to run the commands, then close the command-line window.
/K would leave the command window open after it exited the batch file.
To help you understand the SendKeys commands:
+=[Shift Key]
^=[Control Key]
{esc}=[Escape Key]
{enter}=[Enter Key]
To learn more about using CMD.EXE, type CMD /? at the command prompt.
This is a very untidy and ugly way to do it, but it's the only way I know how using only the tools that come with Windows.

How to execute hidden command in cmd and exit

I have this line:
C:\Windows\system32\cmd.exe start /minimized /wait cmd.exe /c picture.jpg
I use this line in Target field of shortcut.
When I run it, it opens minimized cmd, and picture. What I need is to close that cmd, after opening picture, or to run cmd in hidden mode.
Can anyone help? Thanks in advance.
I am not sure it this may work in your scenario. There are two possible workouts to this.
1) Use a batch script and in the last line, simply add exit to the script. save it as something.bat and run it whenever needed. This will execute the scripts and in the end, the cmd will exit.
2) Alternately you may use want to run the cmd in hidden mode (not minimized). to do this, create a batch job. then pack this batch file using this particular exe compressor - 'iExpress'. (type iexpress under the run box) This has an option to run the scripts in hidden mode. they will run in background.
Isn't
C:\Windows\System32\cmd.exe /c start picture.jpg
doing what you need ?
Why don't you just point the shortcut directly to
picture.jpg
?
Why you need cmd to open a picture?
Just do it like
rundll "C:\Program Files\Windows Photo Viewer\PhotoViewer.dll" "image.png"