Close browser after calling it - batch-file

I have a series of batch files that execute some action on a windows server, and then call a web url that registers on a database that the actions are completed and when.
I call the web file like this
Start "http://the_url"
It works fine, but i noticed that the browser persist in the background processes, but if I log in with the user there's no opened browser's window.
Ho can I close the browser in the same batch file? There's a better way to call an url?

start the process with WMIC to get its pid:
#echo off
for /f "skip=5 tokens=2 delims==; " %%a in ('wmic process call create "chrome.exe url" ') do (
set "pid=%%a"
goto :break
)
:break
echo %pid%
taskkill /pid %pid% /f
May be better approach is to execute directly a webrequest without a browser.
Check winhttpjs.bat which is capable to execute numerous kind of http requests.

You are going to need the Process ID of the Browser to kill it. This might prove problematic if another browser is already open when the Start command is executed. However it is possible by iterating over the output of tasklist like this answer. Once you have this Process ID(PID) you can call taskkill to end it.

Instead of starting a browser, you might be able to just use a Powershell command such as:
powershell Invoke-WebRequest -Uri "http://the_url/"
If performing a GET on the URL is the only thing required, this should do the trick.

Related

Killing a Batch script from another

I have a script that will call other scripts to run then go back to process.
Example:
Call a script to ping and when it completes, go back to script.
My issue is I have a script called Bounce.bat
Here is the code:
#Echo off
set IPADDRESS=127.0.0.1
set INTERVAL=60
:PINGINTERVAL
ping %IPADDRESS% -n 1
timeout %INTERVAL%
GOTO PINGINTERVAL
What I need is the command that will kill the Bounce.bat from inside another batch script.
I was going to try killing CMD but that kills the original script as well.
I Have tried experimenting as well as looking at script examples.
Does anyone know what is needed to kill this bat only?
You can use the Command "taskkill" to stop other processes in Windows.
You can write something like this in your other batch-file:
taskkill /IM MYBATCHFILENAME.bat
You can force to kill the process, if you are using the parameter /f at the end of your command.

Need Batch file help searching for specific string being created in another rolling open session?

Thanks. Thought I'd try writing a batch file to kill another open cmd session that is constantly open churning out lots of scrolling info.
I got a bit carried away and am now outta my league so to speak.
Basically I am trying to search for "£000.00" within a each emerging line of tet that appears in the other running open command window. The running command session is named in the window as C:\windows\system32\cmd.exe but is does have a valid .exe process name in task manager while running and open.
The batch file code below is as far as I've got.
The idea is that when the above string is found in the other process that process/program get closed down them re-launched.
This is as far as I've got.
#echo off
#echo off
title Shut down other process and relaunch
:loop
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
:: #setlocal enabledelayedexpansion
:: #echo off
:: set stringfound=1
find /c "*£000.00*" C:\Windows\system32\cmd.exe && (echo found %date% %time%:~0,-3% >> "%USERPROFILE%\Desktop\Crash_Report.txt"
taskkill /IM ActiveDOSprogram.exe /F
timeout /t 20
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
goto loop
So when I tried this without any variables and in a loop and I think i nearly blew my PC!
Reason I'm stuck is I'm really a novice at this (but trying) and I got as far as I think I need a variable in there somewhere, that only move's to the next line (taskkill+restart) when £000.00 is found in the other process.
Thanks
wingman

Why do not all started applications save the wanted information in the text files as expected?

I am trying to create a batch file on a USB drive that will run an .exe, save its text to a .txt file, and then close the .exe.
I am currently running into a weird problem, only 5 of the 18 .exe's are actually saving their text to a file.
This is the convention I am using to complete my task:
start IEPassView.exe /stext Results/IEPassView.txt
taskkill /f /im IEPassView.exe
start MailPassView.exe /stext Results/MailPassView.txt
taskkill /f /im MailPassView.exe
start MessenPass.exe /stext Results/MessenPass.txt
taskkill /f /im MessenPass.exe
start RouterPassView.exe /stext Results/RouterPassView.txt
taskkill /f /im RouterPassView.exe
start ProtectedStoragePassView.exe /stext Results/ProtectedStoragePassView.txt
taskkill /f /im ProtectedStoragePassView.exe
start DialUpPassView.exe /stext Results/DialUpPassView.txt
taskkill /f /im DialUpPassView.exe
I have 18 of the above blocks in a row all calling different small programs and even though 5 of them actually save the files none of them save a .cfg file as they sometimes do. Any help would be greatly appreciated, thanks.
There are mainly 3 different types of executables:
A console application is reading from stdin or a file and writing to stdout or a file and outputs error messages to stderr.
The processing of a batch file is halted on starting a console application until the console application terminated itself. The correct term is therefore: calling a console application.
The exit code of the console application is assigned to environment variable ERRORLEVEL and can be also directly evaluated for example with if errorlevel X rem do something
Many *.exe in System32 directory of Windows are such console applications, like find.exe, findstr.exe, ping.exe, ...
A GUI (graphical user interface) application is started as new process which means the Windows command processor does not halt batch processing until the GUI application terminates itself.
It is not easily possible to get something from within a command process from such applications. GUI applications are designed for interacting via a graphical user interface with a user and not via standard streams or files with a command process.
A typical example for such applications is Windows Explorer explorer.exe in Windows directory.
A hybrid application supports both interfaces and can be therefore used from within a command process as well as by a user via GUI.
Hybrid applications are rare because not easy to code. The behavior of hybrid applications on usage from within a batch file must be find out by testing.
Example for such applications are Windows Registry Editor regedit.exe or shareware archiver tool WinRAR WinRAR.exe.
It is best to look on simple examples to see the differences regarding starting/calling all 3 types of applications from within a batch file.
Example for console application:
#echo off
cls
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 5
echo.
echo Ping finished pinging the own computer (localhost).
echo.
pause
The command processing is halted until ping.exe terminated itself which takes 4 seconds. There is no need for start or call for such applications, except the console application should be intentionally executed in a separate process.
Example for a GUI application:
#echo off
cls
%WinDir%\Explorer.exe
echo.
echo Windows Explorer opened and is still running!
echo.
pause
This batch file outputs the text and message prompt to press any key immediately after starting Windows Explorer indicating that Windows Explorer was started as separate process and Windows command processor immediately continued on the next lines of the batch file. So although Windows Explorer was started and is still running, the batch processing continued, too.
Example for a hybrid application:
#echo off
cls
"%ProgramFiles%\WinRAR\WinRAR.exe"
echo.
echo User exited WinRAR.
echo.
pause
This batch file starts WinRAR without any parameter (usually not useful from within a batch file) if being installed at all in default installation directory. But batch processing is halted until the user exited WinRAR for example by clicking on X symbol of the WinRAR´s application window.
But opening a command prompt window and executing from within the window
"%ProgramFiles%\WinRAR\WinRAR.exe"
results in getting immediately the prompt back in command window to type and execute the next command. So WinRAR finds out what is the parent process and acts accordingly.
Windows Registry Editor shows the same behavior. Executing from within a command prompt window
%WinDir%\regedit.exe
results in opening Windows Registry Editor, but next command can be immediately entered in command prompt window. But using this command in a batch file results in halting batch processing until GUI window of Windows Registry Editor is closed by the user.
Therefore hybrid applications are used from within a batch file mainly with parameters to avoid the necessity of user interaction.
Okay, back to the question after this brief lesson about various types of applications.
First suggestion is using
Result\TextFileName.txt
instead of
Result/TextFileName.txt
as the directory separator on Windows is the backslash character, except the executables require forward slashes as directory separator because of being badly ported from Unix/Linux to Windows.
Second suggestion is finding out type of application. Is command start really necessary because the applications don't start itself in a separate process and need user interaction to terminate itself?
Note: Command start interprets first double quoted string as title string. Therefore it is always good to specify as first parameter "" (empty title string) on starting a GUI or hybrid application as a separate process. On starting a console application as a separate process it is in general a good idea to give the console window a meaningful title.
And last if the started applications really would need user interaction to terminate, it would be definitely better to either start and kill them after waiting 1 or more seconds between start and kill or start them all, wait a few seconds and finally kill them all at once.
Example for first solution with starting and killing each application separately:
#echo off
setlocal
set TimeoutInSeconds=3
call :RunApp IEPassView
call :RunApp MailPassView
call :RunApp MessenPass
call :RunApp RouterPassView
call :RunApp ProtectedStoragePassView
call :RunApp DialUpPassView
endlocal
goto :EOF
:RunApp
start "" "%~1.exe" /stext "Results\%~1.txt"
set /A RetryNumber=TimeoutInSeconds + 1
%SystemRoot%\System32\ping.exe 127.0.0.1 -n %RetryNumber% >nul
%SystemRoot%\System32\taskkill.exe /f /im "%~1.exe"
goto :EOF
It is also possible to use timeout instead of ping for the delay if the batch file is only for Windows Vista and later Windows versions.
Example for second solution with starting all applications, wait some seconds and kill them finally all:
#echo off
call :StartApp IEPassView
call :StartApp MailPassView
call :StartApp MessenPass
call :StartApp RouterPassView
call :StartApp ProtectedStoragePassView
call :StartApp DialUpPassView
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 6 >nul
call :KillApp IEPassView
call :KillApp MailPassView
call :KillApp MessenPass
call :KillApp RouterPassView
call :KillApp ProtectedStoragePassView
call :KillApp DialUpPassView
goto :EOF
:StartApp
start "" "%~1.exe" /stext "Results\%~1.txt"
goto :EOF
:KillApp
%SystemRoot%\System32\taskkill.exe /f /im "%~1.exe"
goto :EOF
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
goto /?
ping /?
set /?
setlocal /?
start /?
taskkill /?
See also the Microsoft article about Using command redirection operators.
PS: The last two batch code blocks were not tested by me because of not having available the applications.
I suggest killing all tasks at once, at the end of the very end, possibly after a timeout command with a amount of time appropriate to your system's speed. That may help the issue.

How to kill a service in a batch file every 20 seconds?

A task called "FireSvc.exe" (McAffee service) keeps interfering with our app upgrades. I put
> taskkill /f /im "FireSvc.exe"
in a batch file. We continuously run this during installs so the software will successfully upgrade. I'm not positive why this service starts back so often. We have to run from the command line, because in the task manager you get "access denied" when trying to kill this task.
My question is, how would you make this run every 20-30 seconds?
We cannot install any type of non-approved software either. So, theres that...
Thanks for any input.
Here's what we use:
:runagain
taskkill /f /im "FireSvc.exe"
timeout /T 5
goto runagain
You can use vbscript's sleep command in a batch file with cscript like so...
First create a vbscript file (.vbs extension) that contains the following code (don't forget to save it with ANSI encoding otherwise it won't work):
Wscript.sleep 2500
Wscript.exit
Create a batch file in the same directory with this code:
#echo off
:Kill
cscript //NoLogo file.vbs
taskkill /f /im "FireSvc.exe"
GoTo Kill
I currently don't have access to a PC to check if it works so let me know what happens. I still think there might be a cleverer alternative... cheers!
Edit: Btw you can also simulate a sleep command with the ping command like so:
ping localhost -n 1 -w 2500 >nul
And if you are using windows vista or above you can use the timeout command.

start executable in scripts with output redirect

I have a batch script that I use to start and stop a service. The whole script is encapsulated in a subroutine so that i can do output redirect:
call :sub >> C:\Users\Administrator\Desktop\output%LogFilename%.txt
#exit /b 0
:sub
.
start cgsservice.exe -c config\cgs.json
.
The problem that I am having is that after starting the service using the script I CANT run the script again to stop the service. I get the following error:
The process cannot access the file because it is being used by another process.
I know what the issue is but I dont know how to fix it. The issue is the redirect to the log file, since the service is running it wont let me run the script again because it cant access the output%LogFilename%.txt file. Ones I kill the service manually im able to run the script again. Any ideas how to solve this issue with out getting rid of the redirect
You won't be able to "reclaim" the log file while the process is running. Instead you will have to stop the process which has a lock on it. You can automate the stop by adding this line prior to your call to :sub:
REM Stop any running instances of the service.
REM This is needed to release the lock on the log file.
TaskKill /im cgsservice.exe /f
Note this is the same as forcefully terminating the application, so I'm not sure what ramifications it may have. You can try without the /f switch (which will attempt a graceful closing) to see if the process stops this way.
Your other option would be to just have a separate log file for the cgsservice.exe process, but I'm not sure how that would fit into your workflow. To implement this, update the line:
call :sub >> C:\Users\Administrator\Desktop\output%LogFilename%_cgsservice.txt
Note that doing this will cause a lock on the alternate log file until the cgsservice.exe process is terminated. You can use the same TaskKill statement as above to stop it.
If you want to check if cgsservice.exe is not started and then start it, this should do it:
FOR /F "usebackq tokens=* delims=" %%A IN (`TaskList /FI "IMAGENAME eq cgsservice.exe"`) DO (
REM Check against the result of TaskList when no matches are found.
IF "%%A"=="INFO: No tasks are running which match the specified criteria." (
REM Service is not started. Start it now.
START cgsservice.exe -c config\cgs.json
)
)

Resources