Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Powershell (or vbscript) to kill process if more than 1 exists

Posted on 2009-02-12
7
Medium Priority
?
1,785 Views
Last Modified: 2012-05-06
I have a process on a windows 2003 r2 enterprise server that runs once every 10 minutes.  It will occasionally lock up and the following processes will launch and lock up as well.

I'm trying to write a Powershell script (or vbscript) that will run every 10 minutes to check if there is more than 1 process, and if so, kill them all.  It seems pretty simple, but I'm not very experienced in Powershell and am trying to learn.

Any help would be appreciated.
0
Comment
Question by:aml51z
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:speshalyst
ID: 23622859
you can uses this powershell command to kill a running process
http://www.myitforum.com/articles/40/view.asp?id=10097 
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 23622962
This should do it for you
while($true)
{
   Get-Process <name> | kill
   Start-Sleep -Seconds 600
}
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 23622966
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aml51z
ID: 23623653
So this sees if it's running and kills it.

The problem is that one process SHOULD be running.  But if any more than one is running, none should be running, because it has locked up.  My problem is I need a way for the script to determine if there is more than one process with the same name running and only kill them all if that is the case.

something like:
(forgive me, this is just logic and obviously not syntactically correct... still learning)
Get-Process | Where { $_.Name -Eq "ProcName" }
 
# it's gets shady right here...
 
if [ "$input" > "1" ]; then
     Get-Process | Where { $_.Name -Eq "ProcName" } | kill
     echo "$presenttime: locked processes were killed." >> lockedprocesskiller.log
fi

Open in new window

0
 

Author Comment

by:aml51z
ID: 23623784
also, wouldn't using the -name flag be a bit easier than piping to where?

is there a reason this:

Get-Process | Where { $_.Name -Eq "ProcName" }

is used instead of this:

Get-Process -name ProcName

?
0
 
LVL 18

Accepted Solution

by:
BSonPosh earned 2000 total points
ID: 23623974
you should use -name... the where is good if you need to filter on something that is not a parameter.

If you just want processes name "bleh" that have been running more than 10 minutes try this
Get-Process notepad | ?{$_.StartTime -gt (get-date).addminutes(-10)}
 
## Here is example
 
275> Get-Process notepad
 
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     51       2     1244       4684    58     0.08   2068 notepad
    160       5     5664      14096    93    11.72   3444 notepad
     48       2     1364       4024    58     0.92   4488 notepad
     53       2     1888       5608    58    24.22   5776 notepad
     51       2     1244       4628    58     0.08   6444 notepad
     51       2     1448       5240    58     1.81   8100 notepad
 
 
276> Get-Process notepad | ?{$_.StartTime -gt (get-date).addminutes(-10)}
 
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     51       2     1244       4684    58     0.08   2068 notepad
     51       2     1244       4628    58     0.08   6444 notepad

Open in new window

0
 

Author Comment

by:aml51z
ID: 23624095
Awesome!  That should finish it up.  Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question