Link to home
Start Free TrialLog in
Avatar of aml51z
aml51z

asked on

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

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.
Avatar of Speshalyst
Speshalyst
Flag of India image

you can uses this powershell command to kill a running process
http://www.myitforum.com/articles/40/view.asp?id=10097 
This should do it for you
while($true)
{
   Get-Process <name> | kill
   Start-Sleep -Seconds 600
}
Avatar of aml51z
aml51z

ASKER

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

Avatar of aml51z

ASKER

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

?
ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aml51z

ASKER

Awesome!  That should finish it up.  Thanks!