0

I have 4-5 process (like java.exe, javaw.exe etc) having username "OWNER"(suppose). Below is the script that filters the java.exe process and kills it if it belongs to "OWNER". I need your help to modify this so that any process related to "OWNER" would be killed if found.

6
  • @4c74356b41 formatting here means? Commented May 28, 2017 at 11:49
  • Let me know if formatting looks ok or not.. Commented May 28, 2017 at 12:05
  • @{}gwmi win32_process - this doesn't look right Commented May 28, 2017 at 14:01
  • It's working on prod boxes from past 2 years without any issue for java.exe . I want to add feature for more process in this script, need help for that. Commented May 28, 2017 at 14:03
  • 1
    it cannot possibly work, its a typo or a mistake, just try it Commented May 28, 2017 at 14:13

2 Answers 2

5

Just do it with Get-Process:

get-process -IncludeUserName | where username -like $username | stop-process

Basically your whole script can be replaced with this line

Sign up to request clarification or add additional context in comments.

1 Comment

V3 don't have includeusername
0

Get-Process with -IncludeUsername switch is only available in WMF 5.0.

WMI is the option here.

You could probably terminate the process just by checking the owner equals to the corresponding user.

Get-WmiObject -Class Win32_Process | Where-Object -FilterScript { 
$_.GetOwner.User -eq "$Owner" } | Invoke-WmiMethod -Name Terminate

Edit: The above code is a one liner, you could save the out put of Get-WmiObject in a variable and for foreach through the collection to print the process id and call the terminate() method instead of using Invoke-WmiMethod.

Note:This code is not tested

3 Comments

This seems to be good idea . Although I first want to print the process ID and want to kill that.. How to do that with the command you typed ..
You will not get a ready made script for your task from StackOverflow, I have edited the Answer by giving you max hint.
In the actual script I pasted I want that to be used as some dependency on that section . Hope you got the point . I will try to club all the help you guys provided .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.