Matt Smith
Matt Smith

Reputation: 23

Script to Check for a Remote Process and “kill” it If Exists

I need to create a script that will be executed on the 2K12R2 directory server and needs to

  1. Check both of the Windows 10 workstations for a remote process (Calculator.exe) and kill/terminate the process if it exists.

a. The script should use a for loop that reads in a text file called hosts.txt that contains the hostnames of the Windows 10 workstations

b. The script should use tasklist and taskkill or a wmic query to enumerate the process (Calculator.exe) and kill it.

This is what I have so far but I'm unsure on how to do a for loop to check a text file for the host names.

echo off

tasklist /fi "imagename eq calculator.exe" |find ":" > nul

if errorlevel 1 taskkill /f /im "calculator.exe"&exit

Upvotes: 2

Views: 1266

Answers (1)

Compo
Compo

Reputation: 38719

You can do it in a single line, no For loop required:

Using WMIC

@WMIC /Node:@hosts.txt Process Where "Name='calc.exe'" Call Terminate

Depending upon privileges, you may need to include /User:privilegedusername and /Password:privilegeduserpassword.


Or you could use a For loop and TaskList/TaskKill:

@For /F "Tokens=*" %%A In (hosts.txt) Do @TaskList /S %%A|Find /I "calc.exe">Nul&&@TaskKill /S %%A calc.exe /F

You may need to use both the /U and /P options with both TaskList and TaskKill, enter each command followed by /? at the prompt for usage information.

Upvotes: 3

Related Questions