Trying to kill tasks that have been started in a file - windows

So I am trying to start a butch of programs at once but then I want to to be able to kill all those programs at a later time. So I have created two bat files one to start the programs in the file and one to stop them.
This is the bat file that should stop them.
for /r "." %%a in (*.exe) do taskkill /IM %%~nxI
pause
It gives me an error that file was not found here is the error that it gave me. > ERROR: Invalid argument/option - 'D:\FileName'
Here is the code that starts them:
for /r "." %%a in (*.exe) do start "" "%%~fa"
pause

To kill all the tasks in the file:
for /r "." %%a in (*.exe) do taskkill /IM "%%~nxa"
pause

Related

How do query and then kill a service in a batch file (newbie)

I am trying to kill specific services using the PID from "SC QUERYEX wuauserv". But I don't know how to pull the PID shown in the results to then run "taskkill /pid /f [PID]". I am trying to make a batch file which I can use on multiple machines remotely.
I have tried a couple of suggestions made in other similar questions found on google, but wuauserv is not being killed for some reason.
# echo off
cmd /c FOR /F "usebackq tokens=2 skip=3" %%i IN (tasklist /fi "services eq wuauserv") DO taskkill /PID %%
pause
The above is what I have, but it's not finding the specific service in the task list. Can anyone assist?
Essentially, because you specified usebackq you need to put your Command within backticks ie: IN (`Command`) DO () you also need to include the variable letter you specified earlier in the ending section which you have, right now it is just %% given you set up i as the variable letter it should be %%i.
Also the CMD /C portion is just not needed at all.
Thats said, just drop the UseBackQ it isn't necessary, here is yoru code cleaned up a little.
#(
SETLOCAL EnableDelayedExpansion
echo off
)
FOR /F "Tokens=2" %%I IN ('
Tasklist /fi "Services eq wuauserv"
^| FIND /I "wuauserv"
') DO (
ECHO Killing PID %%I
Taskkill /PID %%I
)
PAUSE

unable to find .bat pid for killing

I am unable to call a bat process within another bat.
This is the situation.
I have a n1.bat that basically contains:
TITLE "n1.bat"
...
...
start /b /MIN "n2.bat"
...
The n2.bat contains:
TITLE "n2.bat"
...
...
start someother.bat
start another.bat
exit
Now I use a third bat (n3.bat) that must kill everything.
TITLE "n3.bat"
...
TASKKILL /F /T /PID n1_PID
...
exit 0
Unfortunately when n3.bat ends a window called n1.bat - n2.bat remains active (the echo output belongs to n2.bat).
I tried to kill it in some ways:
1- Tried to get the process pid from tasklist -> there is no process called n2.bat or similar
TASKLIST /V /NH>Tasks.txt
FOR /F "tokens=2 delims= " %%n2_pid IN ('FINDSTR n2.bat Tasks.txt') DO SET PID=%%n2_pid
TASKKILL /PID %n2_pid%
2- Trying to use TASKKILL /F /FI "WINDOWTITLE eq n2.bat" -> no process found
I tried the previous solutions also starting n2.bat with
start /min "n2.bat" (so without /b)
with no success.
The only way I manage to kill it is to calling
TASKKILL /F /IM cmd.exe
that I really would like to avoid since it obviously kills all opened cmd.
Any ideas to retrieve the guilty pid?

I have a Batch file that runs perfectly fine on a local machine, but doesn't run through SCCM

#echo off
taskkill /IM Notebook.exe /F
taskkill /IM ResponseSoftwareService.exe /F
taskkill /IM DesktopMenu.exe /F
del "C:\ProgramData\FLEXNet\*" /A /Q /F
"C:\Program Files (x86)\Common Files\SMART Technologies\SMART Product Update\activationwizard.exe" --puid education_bundle --m=4 --v=3 --a --pk="Key input here"
This is the code I placed the actual key where it says Key input here....But like I said when I run this batch file locally everything runs correctly. but when I put it in the Task sequence of SCCM it doesn't run correctly and I have to run this batch file any way. Any ideas or tips?
What return code do you get?
It might also be worth redirecting the output of each command and its exit code to get more information, so for example:
taskkill /IM Notebook.exe /F >> C:\Windows\Temp\mylogfile.log
echo %ERRORLEVEL% >> C:\Windows\Temp\mylogfile.log
taskkill /IM ResponseSoftwareService.exe /F >> C:\Windows\Temp\mylogfile.log
echo %ERRORLEVEL% >> C:\Windows\Temp\mylogfile.log
And so on.
That should then tell you if it's permissions related or any other errors that arise.
If you're stilling having problems afterwards then post your results and I'll be happy to advise further.

Stop Music Once Playing

I am trying to get a song to play in the background of Windows, after a little looking I found this:
#echo off
set file=RRLJ.mp3
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs
This works well for starting the song but I have no way of stopping it from a .bat file. The only way I found to cut it short is to open task manager and close it from the processes.
I have tried:
taskkill /im wscript.exe
But I keep getting something in the window saying:
Success: Sent termination sidnal to the process "wscript.exe" with PID 185448
but the music continues to play until I manually end it with task manager
I'd use /T switch (Tree kill): terminates the specified process and any child processes which were started by it.
Here is a script to find ProcessID to terminate exactly needed process only using PID:
#ECHO OFF >NUL
for /F "usebackq tokens=*" %%G in (
`wmic process where "CommandLine like '%%sound.vbs%%' AND Caption like '%%script.exe%%'" get ProcessID/value ^|find /I "="`
) do (
rem echo %%G
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID %%H
)
Note taskkill command is echoed merely... Remove echo when debugged.
This work for me, and is only one line.
Taskkill /IM "wscript.exe" /F>nul 2>&1

Batch file - Closing separate cmd windows opened by a batch file process

I am putting together a batch file which does the following:
Start Selenium test hub
Start Selenium test node
Run Selenium test script
Open test result xml doc.
Close Selenium hub and node
The Selenium hub and node are opened by calling 2 batch files using the START command so they open in their own cmd windows. I am all good with my process up to point 5), closing the two cmd windows containing the hub and node.
I know you can close all "cmd.exe" processes but that seems way to much of a blunt instrument. Is there a way of identifying or keeping a handle on the hub and node cmd windows opened by my batch script so that I can then just close those two only?
Thanks.
As an alternative to the wonderful npocmaka answer, if you know or can set the title of the started Selenium windows, can use this information to retrieve and filter the list of tasks with those titles.
When output to console is correct, remove the echo command from taskkill line
#echo off
rem Prepare environment
setlocal enableextensions
rem Configure list of window titles
set titles="seleniumHUB" "seleniumNODE"
rem For each title in title list
for %%t in (%titles%) do (
rem Get the list of task with the indicated window title.
rem The list is get from tasklist, in verbose mode and in csv format
rem The last column in the list is the window title
for /f "tokens=1,2 delims=," %%a in (
'tasklist /fi "imagename eq cmd.exe" /v /fo csv ^| findstr /i /r /c:"%%~t[^,]*$"'
) do (
echo taskkill /pid %%~b
)
)
rem Clean
endlocal
Try something like this:
#echo off
for /f "tokens=2 delims=;= " %%a in ('wmic process call create "cmd.exe /c C:\selenium.bat"^,"c:\workdir" ^| find "ProcessId"') do (
set "cmd_pid=%%a"
)
taskkill /PID %cmd_pid% /f
with wmic process call create "cmd.exe /c C:\selenium.bat" you can start a process and get its PID and when you to kill it you can use taskkill /PID %cmd_pid% /f

Resources