ConEmu: How do I make a task automatically close after completing - conemu

I have the following task set up in ConEMU
TASKKILL /IM iexplore.exe /F
After the task runs, it leaves a console window open with a confirmation dialog "Press Enter or Esc to close console window".
Is there a way to make my task automatically close it's tab after performing its operations.
I tried using -cur_console and -new_console but these don't seem to do what I need.

There is a setting Confirm close for that in Settings > Integration > Default term:

According to this documentation you can use the :n switch:
n - disable ‘Press Enter or Esc to close console’
For example, the following task opens a new tab, performs a git pull on master and then automatically closes again:
-new_console:n cd C:\my_git_repo & git fetch --all & git checkout master & git pull
Watch out as it closes the tab even on non-successful status codes.

Related

how to close a dialog box in batch file by clicking OK

I have a batch script which runs a command:
certreq -submit -attrib "CertificateTemplate:WebServer" CSR.csr CRT.crt
This produces a dialog box to named as "Select Certificate Authority" with OK and cancel button, I need to proceed with OK to complete that command. Can I do it with command to close the box by processing it with OK?
you can try with sendkeys.bat:
call sendkeys.bat "certificate" "{enter}"
The first argument is the title of the window you want to process and the second is the string you want to send to it.Here you can find an info what special characters you can send.
I used autoit tool to get it done. It is simple to use.
Below are the autoit commands.
WinWaitActive("Certification Authority List")
WinWaitActive("Certification Authority List")
ControlClick("Certification Authority List","OK","[CLASS:Button; INSTANCE:1]")

How to generate key strokes from a batch file?

I want to create a batch file to do the following:
Start selenium server(webdriver-manager start)
Run Protractor tests(protractor conf.js)
Stop Selenium server()
This needs 2 different command prompts since webdriver-manager start will keep running and simultaneously the tests need to be executed
I have achieved the following so far. I have created a .bat file with the following contents:
start runTests.cmd
webdriver-manager start
Ctrl-C(**DOES NOT WORK**)
However, I am not able to figure out a way to shutdown the Selenium server(which is achieved by pressing Ctrl+C on the same window on which the webdriver-manager start command is executed)
You can generate keystrokes using VB Script, which can be called from a batch file.
I followed this post: http://www.w7forums.com/threads/f5-key-via-batch-file.18046/, substituting {F5} with ^{C} for Ctrl+C. So my file looks like:
Set objShell = CreateObject("WScript.Shell")
objShell.SendKeys "^{C}"
You need to save this file with a ".vbs" extension.
I also found from this answer VBScript - switching focus to window using AppActivate how to set the focus to another window (which you know the title of). Doing this first, you can direct your keystroke to the appropriate window, so:
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate "Untitled - Notepad"
objShell.SendKeys "^{C}"
I experimented with an empty instance of Notepad, so you'll need to change the window title string appropriately. (Ctrl+C doesn't do anything in Notepad, but Alt+F4 closes it, so I used "%{F4}" to test it.)
Then you can simply call the VBS file from your batch file to run it.

Run Batch File On Start-up

Is there a way to start multiple programs in a batch file on system start-up? In addition to that, in that batch file, I would like to be able to say: Once I execute a program, wait until that program completely loads, and execute the next listed program.
Any help would be appreciated.
I had the same issue in Win7 regarding running a script (.bat) at startup (When the computer boots vs when someone logs in) that would modify the network parameters using netsh. What ended up working for me was the following:
Log in with an Administrator account
Click on start and type “Task Scheduler” and hit return
Click on “Task Scheduler Library”
Click on “Create New Task” on the right hand side of the screen and set the parameters as follows:
a. Set the user account to SYSTEM
b. Choose "Run with highest privileges"
c. Choose the OS for Windows7
Click on “Triggers” tab and then click on “New…”
Choose “At Startup” from the drop down menu, click Enabled and hit OK
Click on the “Actions tab” and then click on “New…”
If you are running a .bat file use cmd as the program the put
/c .bat
In the Add arguments field
Click on “OK” then on “OK” on the create task panel and it will now
be scheduled.
Add the .bat script to the place specified in your task event.
Enjoy.
To run a batch file at start up: start >> all programs >> right-click startup >> open >> right click batch file >> create shortcut >> drag shortcut to startup folder.
The path to the folder is : [D|C]:\Profiles\{User}\‌​AppData\Roaming\Micro‌​soft\Windows\Start Menu\Programs\Startu‌​p
Go to Run (WINDOWS + R) and
Type
shell:startup, paste your .bat file there !
To start the batch file at the start of your system, you can also use a registry key.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Here you can create a string. As name you can choose anything and the data is the full path to your file.
There is also the registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
to run something at only the next start of your system.
There are a few ways to run a batch file on start up. The one I usually use is through task scheduler. If you press the windows key then type task scheduler it will come up as an option (or find through administerative tools).
When you create a new task you can chose from trigger options such as 'At log on' for a specific user, on workstation unlock etc. Then in actions you select start a program and put the full path to your batch script (there is also an option to put any command line args required).
Here is a an example script to launch Stack Overflow in Firefox:
#echo off
title Auto launch Stack Overflow
start firefox http://stackoverflow.com/questions/tagged/python+or+sql+or+sqlite+or+plsql+or+oracle+or+windows-7+or+cmd+or+excel+or+access+or+vba+or+excel-vba+or+access-vba?sort=newest
REM Optional - I tend to log these sorts of events so that you can see what has happened afterwards
echo %date% %time%, %computername% >> %logs%\StackOverflowAuto.csv
exit
RunOnce
RunOnce is an option and have a few keys that can be used for pointing a command to start on startup (depending if it concerns a user or the whole system):
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
setting the value:
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce" /v MyBat /D "!C:\mybat.bat"
With setting and exclamation mark at the beginning and if the script exist with a value different than 0 the registry key wont be deleted and the script will be executed every time on startup
SCHTASKS
You can use SCHTASKS and a triggering event:
SCHTASKS /Create /SC ONEVENT /MO ONLOGON /TN ON_LOGON /tr "c:\some.bat"
or
SCHTASKS /Create /SC ONEVENT /MO ONSTART/TN ON_START /tr "c:\some.bat"
Startup Folder
You also have two startup folders - one for the current user and one global.
There you can copy your scripts (or shortcuts) in order to start a file on startup
::the global one
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
::for the current user
%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
1. Copy the following lines to Notepad.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Users\toto\your_file.bat" & Chr(34), 0
Set WshShell = Nothing
Note: Replace the batch file name/path accordingly in the script according to your requirement.
2. Save the file with .VBS extension, example launch_bat.vbs
3. Create new .bat file, in our case your_file.bat
4. Write the content of your .bat file.
Example:
#echo off
php c:\laragon\www\my_app\artisan serve --host=127.0.0.1 --port=8000
5. Run your_file.bat and ejoy :)
If your Windows language is different from English, you can launch the Task Scheduler by
Press Windows+X
Select your language translation of "Computer Management"
Follow the instruction in the answer provided by prankin
Another option would be to run the batch file as a service, and set the startup of the service to "Automatic" or "Automatic (Delayed Start)".
Check this question for more information on how to do it, personally I like NSSM the most.

How to keep the shell window open after running a PowerShell script?

I have a very short PowerShell script that connects to a server and imports the AD module. I'd like to run the script simply by double clicking, but I'm afraid the window immediately closes after the last line.
How can I sort this out?
You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.
One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g. PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
Per-script Fix: Add a prompt for input to the end of your script file. e.g. Read-Host -Prompt "Press Enter to exit"
Global Fix: Change your registry key by adding the -NoExit switch to always leave the PowerShell Console window open after the script finishes running.
Registry Key: HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command
Description: Key used when you right-click a .ps1 file and choose Open With -> Windows PowerShell.
Default Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1"
Desired Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& \"%1\""
Registry Key: HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command
Description: Key used when you right-click a .ps1 file and choose Run with PowerShell (shows up depending on which Windows OS and Updates you have installed).
Default Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
Desired Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & \"%1\""
See my blog for more information and a script to download that will make the registry change for you.
Errr...
I should have known:
powershell -noexit <path\script>
and that's all there's to it :)
The solution below prevents the script from closing when running Powershell ISE and allows the script to close otherwise.
# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost")
{
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}
Just add pause on a new line at the bottom of the script, as in a batch file.
In my own case, I wanted to add powershell to context menu on windows 7. That is right clicking on a folder or inside a folder to get a menu to launch Powershell window without it closing after launch. The answers here helped me do just that and I want to share it here incase it helps someone else.
Launch registry editor by pressing WIN + R
type regedit.exe and hit enter
Navigate to HKEY_CLASSES_ROOT\Directory\Background\shell
Right click on shell and create a key give it a name e.g PowershellHere
On the right pane, double click on Default and provide a descriptive name e.g Powershell Here
Right click on the PowershellHere key you created earlier and create a new key and name it "command" please make sure you name it exactly so but without the quotes.
On the right pane, double click on Default and then type the command below
C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit -Command CD '"%1"'
-noexit flag makes sure that the Powershell windows does not close again immediately after launch
'"%1"' flag represents the folder you right clicked
-Command CD '"%1"' will ensure the Powershell changes into the right clicked directory.
To make the right click work inside a folder meaning right clicking an empty space inside a folder, repeat the steps but this time, the registry location is:
HKEY_CLASSES_ROOT\Directory\shell
And the command is:
C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit
Tested on windows 7 ultimate sp1 but I think I might work for later versions of Windows as well
You can add pause at the end of your script if you just want the window to stay open, or you can add powershell if you want to be able to run commands afterwards (obviously don't do the second option if anyone else will use your code).

Prevent batch file in CMD from closing without using Pause

Currently I want to run a batch file that fires the command git log and show me that log.
After that I need to be able to commit and view the status so this prompt may not disappear after a key press.
I've searched the net and the only answer people have is pause which close the prompt after a keypress.
Does anyone have the solution for me? Currently I made a shortcut to cmd.exe and made the target my folder, but I want to execute some commands also.
Thanks in advance.
This (below) tested OK in Windows 7. To exit the window it creates, type "exit" when done.
start cmd /K "cd \[the-target-folder] && git log"
Where:
[the-target-folder] you replace with your target folder
Note:
&& lets you run two commands on one line
/K is a parameter to the cmd shell program which which carries out the command specified by string and remains.

Resources