Script to end process with a specific name - scripting

I have to kill two processes with the same image name (system and my local user name) every day. Is there a script that I could write or do I have to go to the task manager every time windows starts up?

Taskkill is the command you're looking for. It can even take pretty specific filtering arguments.

A bat file would do it, and could say
taskkill /im regedit.exe
(taskkill /? has other examples)
a vbs file with this line does it too
CreateObject("Wscript.Shell").Run "taskkill /im regedit.exe", 0, False
more invisibly done than just having windows just normally running a batch file

Related

How to change windows theme (or at least the background) using the command line or scripts

I am trying to make it so that the theme (or at least the background) changes when I switch desktop view. I have found some answers but most if not all are about old versions of windows and do not work for windows 10. The scripting tool I am using is AHK. Thank you in advance.
Its possible to change the background using this command:
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d wallpaper_path /f
Changing the theme is not that direct, you might want to run the following command. What it does is that it will show you a dialog where you can pick the theme you want. (It may also reset your theme to default)
rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,#Themes /Action:OpenTheme /file:"C:\Windows\Resources\Themes\aero.theme"
It's although possible to change the theme using this command:
call <path-to-your-theme>
You close the opened window using this command:
taskkill /IM ApplicationFrameHost.exe
I came across this question while looking to do the same thing for my company's computer image. The command below worked well for me, keep in mind that the taskkill part would kill all settings windows
C:\PATH\TO\THEME\FILE.theme & timeout /t 03 /nobreak > NUL & taskkill /f /im systemsettings.exe
This will launch the theme file which will apply the theme but also open the Setting app/window, but then it will wait for the timeout (set to 3 seconds in the command above) then kill the Settings app/window. Tested on Windows 10 Enterprise x64 v1909. Also depending on the performance of the computers you might be able to lower the timeout from 3 seconds.
Martin's answer didnt work for me, but after some reading it believe its because of changes made with either v1809 or v1903

Stop a recurring trigger in Windows Task Scheduler until the next day

We have scheduled a Windows Task to kick off some custom code (as an executable) that checks for existence of a file every 15 minutes.
If the file is eventually found, our service does some processing on it. Here's the rub: after the file is processed, the business requires that Task Scheduler stops these 15-minute checks until the next day.
Is there a way to insert logic into Windows Task Scheduler to stop running its trigger once some condition is met? Or is there a better way to architect this process?
We are using Windows Server 2008 R2 Standard SP1 to run this.
How to end a scheduled task:
A. To terminate a currently running task (i.e. one that shows up in Task Scheduler -> "Display All Running Tasks) from the command line:
schtasks.exe /End /TN "My task name"
B. To stop a scheduled task from being triggered to run in the future:
schtasks.exe /Change /TN "My task name" /DISABLE
C. To re-enable the task:
schtasks.exe /Change /TN "My task name" /ENABLE
More details are available by typing: schtasks.exe /Change /?
D. To ask a process to terminate (here: Notepad, but use the name displayed in Task Manager -> Processes\Image Name) that does not terminate when you run A. above:
taskkill.exe /IM Notepad.exe
E. To forcefully terminate a process that does not terminate when you run D. above:
taskkill.exe /F /IM Notepad.exe /T
Note: Using taskkill isn't a clean way of ending a process. You can read more in this post and in the article it links to.
How to set this up in Task Scheduler for your described situation:
Instead of having your task run the custom .exe directly, change it to run a script (e.g. a batch file, PowerShell script, Python script, etc.) which in turn:
Triggers the custom exe, then
Tests whether the file was found and "processed", then
Invokes command B. above to stop it from running again.
Add a second task that runs every morning (check "Run whether user is logged on or not) that re-enables the scheduled task by invoking command C. above.
Note: To invoke schtasks.exe requires elevated privileges, so set the task that executes that command to "Run with highest privileges".
How to detect that the file has been processed:
Have your custom .exe to add a Registry entry when it ran successfully. Your script can look for that entry using e.g. reg.exe (type REG QUERY /? for details).
There are other ways (e.g. posting a Windows event, sending a message, etc.), but using the Registry is an easy mechanism for a simple script to use.
Don't disable the task from directly your .exe. Keep the application and its invocation separate. That will also save you from having to recompile if you want to disable the task differently later.

How to stop a process automatically via a batch script

How can I check if a process is running from a batch/cmd file? And If process is running, how do I stop the process automatically?
Like a cmd script thingy, can someone give me some hints or help me make that kind of script.
Example (pseudo code):
If calc.exe is running
Stop calc.exe
I want something like this:
#echo off
:x
PATH=C:\Windows\System32\calc.exe
If calc.exe is ON goto Process_Stop
:Process_Stop
net stop calc.exe
goto x
First off, you have the wrong command to stop a process like calc.exe. NET STOP will stop a service, but not a normal program process. You want TASKKILL instead.
Second - If you know you want to kill the process if it exists, then why do you think you have to check if it is running first before you kill it? You can simply attempt to kill the process regardless. If it doesn't exist, then an error is generated, and no harm done. If it does exist, then it is killed with a success message.
taskkill /im calc.exe
If you don't want to see the error message, then redirect stderr to nul using 2>nul. If you don't want to see the success message either, then redirect stdout to nul using >nul.
taskkill /im calc.exe >nul 2>nul
If you want to take action depending on the outcome, then you can use the conditional && and || operators for success and failure.
taskkill /im calc.exe >nul 2>nul && (
echo calc.exe was killed
) || (
echo no process was killed
)
Or you could use the ERRORLEVEL to determine what to do depending on the outcome.
A more readable and simpler command is something like this:
taskkill /F /FI "IMAGENAME eq calc.exe"
This methodology never returns an error code if the process isn't running where the /IM switch WILL return an error if the process is not running. You can also use wild cards like:
taskkill /F /FI "IMAGENAME eq calc*.exe" to kill any process that starts with 'calc' and ends with '.exe' in the name.
Check out tasklist command - This command gives you a list of tasks and services that are running. Check if the task that you are interested is running using regular expression
Then use taskkill command to kill the service or task.

Remotely installing patches

I need to know how to run a batch script that will go to a shared location on the network, put the user name and password in and run a specified file. I'm not great with scripts and I can't find any coherent answers so far. I'm trying to run software patches on MANY systems and trying to save a little time with either going to each box individually and tossing in the patch disk or using Languard to click each patch (up to 155) individually.
So far I've got the 'net use' command to connect to the share but can't get it to use the user name or password, let alone get it to execute the patches. Could someone put this in a coherent format please? I am getting alot of the syntax, which is great but they always use extra symbols and asterisks which can be a little confusing. Perhaps an example with example names? Thanks!
PsExec can do what you want
http://technet.microsoft.com/en-us/sysinternals/bb897553
usage: psexec \\computer [-u username [-p password]] [-s] [-c [-f]] [-d] program [arguments]
-u Specifies optional user name for login to remote computer.
-p Specifies optional password for user name. If you omit this you will be prompted to enter a hidden password.
-s Run remote process in the System account .
-c Copy the specified program to the remote system for execution. If you omit this option then the application must be in the system's path on the remote system.
-f Copy the specified program to the remote system even if the file already exists on the remote system.
-d Don't wait for application to terminate. Only use this option for non-interactive applications.
Examples
The following command launches an interactive command prompt on \\marklap:
psexec \\marklap cmd
This command executes IpConfig on the remote system with the /all switch, and displays the resulting output locally:
psexec \\marklap ipconfig /all
This command copies the program test.exe to the remote system and executes it interactively:
psexec \\marklap -c test.exe
Specify the full path to a program that is already installed on a remote system if its not on the system's path:
psexec \\marklap c:\bin\test.exe
src: http://www.governmentsecurity.org/forum/index.php?showtopic=1030
I'm on linux right now so I can't test this.
for /l %%c in (1,1,254) do start psexec \\192.168.1.%%c -d -u administrator -p pass "net use Z: \\yourserver\user$ && Z:\update.exe && net use Z: /delete"
You could schedule the PSEXEC commands on the 70+ machines using scheduled tasks. The command line interface for that is SCHTASKS, so you could schedule them all at once from your administrator machine.
C:\Windows\system32>schtasks /?
SCHTASKS /parameter [arguments]
Description:
Enables an administrator to create, delete, query, change, run and
end scheduled tasks on a local or remote system.
Parameter List:
/Create Creates a new scheduled task.
/Delete Deletes the scheduled task(s).
/Query Displays all scheduled tasks.
/Change Changes the properties of scheduled task.
/Run Runs the scheduled task on demand.
/End Stops the currently running scheduled task.
/ShowSid Shows the security identifier corresponding to a scheduled task name.
/? Displays this help message.
Examples:
SCHTASKS
SCHTASKS /?
SCHTASKS /Run /?
SCHTASKS /End /?
SCHTASKS /Create /?
SCHTASKS /Delete /?
SCHTASKS /Query /?
SCHTASKS /Change /?
SCHTASKS /ShowSid /?

How stops webdev.webserver.exe

how can stops WebDev.WebServer.exe process without kill it??
I can do this:
taskkill /F /IM WebDev.WebServer.exe -works on Windows XP- but another solution without kill the proccess ???
Yeap, you are right, it will kill all process & ASP.NET service
The problem, actually, is how to kill WebDev.WebServer40.EXE, which was run with specified port.
f.e. I have:
WebDev.WebServer40.exe /port:10254 /path:"D:...\Web1"
WebDev.WebServer40.exe /port:10512 /path:"D:...\Web2"
and taskkill /F /IM WebDev.WebServer40.exe
will close all my sites, when I want only one of them
p.s. The Author means stop process == kill process, don't mind
Stopping a process is killing it.
Why do you need to "stop" the process without killing it? What do you mean by "stopping" the process?
You can use this tool to pause and resume processes.
Webdev.WebServer.exe is the development web server used by Visual Studio to allow quick debugging of ASP.NET applications.
Visual studio will stop it for you when it is not needed. What exact problem are you having that you want to stop it explicitly?