?
Solved

block /terminate process

Posted on 2008-10-03
12
Medium Priority
?
907 Views
Last Modified: 2012-05-05
suppose there are two processes running. one is winword.exe and other excel.exe
what code i should add which check either any of these two processes are running or not . and if running terminate that process and if both are running terminate both. after turmination block running of both.
also what code will unblock .
0
Comment
Question by:ankur3020
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 7

Expert Comment

by:Hubasan
ID: 22636304
Hi ankur3020,

Here is the code to check and terminate the winword.exe and excel.exe processes.

As for blocking them you can try and rename their executables and then change them back afterward.
Set oWS = CreateObject("wscript.shell")
 
'Check if the Winword process is running
sComputer = "."
Set oWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcesses = oWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'winword.exe'")
 
For Each oProcess In colProcesses
	If colProcesses.Count = 0 Then
		oWS.Popup "Winword process is not running!", , ,vbInformation
	Else
		sRes = oWS.Popup( "Detected active Winword.exe process..." & vbCrLf & vbCrLf &_
		"Would you like to terminate Winword.exe process?? ", , ,vbInformation+vbyesno)
		If sRes = vbYes Then
			oProcess.Terminate
			WScript.Quit
		Else
			
		End If
	End If
 
Next
 
'Check if the Excel process is running
Set oWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcesses = oWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'excel.exe'")
 
For Each oProcess In colProcesses
	If colProcesses.Count = 0 Then
		oWS.Popup "Excell process is not running, Exiting script!", , ,vbInformation
	Else
		sRes = oWS.Popup( "Detected active Excel.exe process..." & vbCrLf & vbCrLf &_
		"Would you like to terminate Excel.exe process?? ", , ,vbInformation+vbyesno)
		If sRes = vbYes Then
			oProcess.Terminate
			WScript.Quit
		Else
			
		End If
	End If
 
Next

Open in new window

0
 
LVL 7

Assisted Solution

by:Hubasan
Hubasan earned 300 total points
ID: 22636334
There was a small mistake in previous code, here is the corrected version:
Set oWS = CreateObject("wscript.shell")
 
'Check if the Winword process is running
sComputer = "."
Set oWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcesses = oWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'winword.exe'")
 
For Each oProcess In colProcesses
	If colProcesses.Count = 0 Then
		oWS.Popup "Winword process is not running!", , ,vbInformation
	Else
		sRes = oWS.Popup( "Detected active Winword.exe process..." & vbCrLf & vbCrLf &_
		"Would you like to terminate Winword.exe process?? ", , ,vbInformation+vbyesno)
		If sRes = vbYes Then
			oProcess.Terminate			
		Else
			
		End If
	End If
 
Next
 
Set oWMIService = Nothing
 
'Check if the Excel process is running
Set oWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcesses = oWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'excel.exe'")
 
For Each oProcess In colProcesses
	If colProcesses.Count = 0 Then
		oWS.Popup "Excell process is not running, Exiting script!", , ,vbInformation
	Else
		sRes = oWS.Popup( "Detected active Excel.exe process..." & vbCrLf & vbCrLf &_
		"Would you like to terminate Excel.exe process?? ", , ,vbInformation+vbyesno)
		If sRes = vbYes Then
			oProcess.Terminate
			WScript.Quit
		Else
			
		End If
	End If
 
Next

Open in new window

0
 

Author Comment

by:ankur3020
ID: 22640706
thanks your code is working perfect.

can you amend the code a little. is it possible that instead of terminating one particualr exe of file i would able to terminate all the exes that starts with one common word.i.e if there are three processes running whoes name starts with win..., so it is possible code can check all processes running with "win" word and ignore remaining characters of file name and terminate all.

like when u search win*.exe it look for all files that have same begining.

pls provide code if possible.
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!

 
LVL 5

Expert Comment

by:si_shamil
ID: 22648203
I've changed a script to fit your request
You may specify % wildcard (ex. Win%.exe)

Let me know if it works for you:
strProcces = InputBox("Enter proccess name, you may specify % wildcard (ex. Win%.exe)")
 
 
 
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
 
Set oWS = CreateObject("Wscript.Shell")
 
Set colProcesses = oWMIService.ExecQuery("Select * from Win32_Process Where Name LIKE '" & strProcces & "'") 
 
 
 
If colProcesses.Count = 0 Then
 
    oWS.Popup "No procceses found...",,, vbInformation
 
Else
 
    For Each oProcess In colProcesses
 
        sRes = oWS.Popup("Detected active " & oProcess.Name & " process..." & vbCrLf & "Terminate it?",,, vbInformation + vbYesNo)
 
        If sRes = vbYes Then oProcess.Terminate
 
    Next
 
End If
 
 
 
Set oWMIService = Nothing

Open in new window

0
 
LVL 5

Accepted Solution

by:
si_shamil earned 200 total points
ID: 22648219
Sorry the snippes pasted with extra empty lines :(

use this one instead.
You may specify % wildcard (ex. Win%.exe)

Let me know if it works for you:
strProcces = InputBox("Enter proccess name, you may specify % wildcard (ex. Win%.exe)")
 
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set oWS = CreateObject("Wscript.Shell")
Set colProcesses = oWMIService.ExecQuery("Select * from Win32_Process Where Name LIKE '" & strProcces & "'") 
 
If colProcesses.Count = 0 Then
    oWS.Popup "No procceses found...",,, vbInformation
Else
    For Each oProcess In colProcesses
        sRes = oWS.Popup("Detected active " & oProcess.Name & " process..." & vbCrLf & "Terminate it?",,, vbInformation + vbYesNo)
        If sRes = vbYes Then oProcess.Terminate
    Next
End If

Open in new window

0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22649580
Sure here is the modified code that can detect "win" inside a process name and give you the option to kill that process:

If you want to change "win" to something else, then just change "sName" variable instead of "win" to what ever you want.
Const cTitle = "Detect and Kill Processes""
 
Set oWS = CreateObject("wscript.shell")
 
sName = "win"
 
'Check if the Winword process is running
sComputer = "."
Set oWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcesses = oWMIService.ExecQuery _
    ("Select Name from Win32_Process")
 
For Each oProcess In colProcesses
	sProcess = oProcess.Name
	If InStr(UCase(sProcess), UCase(sName)) Then
		sRes = oWS.Popup( "Detected active " & sProcess & " process..." & vbCrLf & vbCrLf &_
		"Would you like to terminate " & sProcess & " process?? ", ,cTitle ,vbInformation+vbyesno)
		If sRes = vbYes Then
			bWinwordKill = True
			oProcess.Terminate			
		End If	
	End If
Next

Open in new window

0
 
LVL 7

Assisted Solution

by:Hubasan
Hubasan earned 300 total points
ID: 22649704
Here is little revised code that will notify you if it doesn't find any processes defined by sName string.
So for example if you try to find "zxx" in the process name and it is not found, script will tell you that it didn't find any processes with that string in it's name.
By the way this script doesn't just look at first couple of the letters to find a process name you are looking for. It looks in the entire process name for "win" or what ever you place in there. So if you have a process running named "singularwin.exe" that one will be detected by the scirpt and it will ask you do you want to kill it. :-)
Const cTitle = "Detect and Kill Processes""
 
Set oWS = CreateObject("wscript.shell")
 
sName = "win"
 
'Check if the Winword process is running
sComputer = "."
bDetected = False
Set oWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcesses = oWMIService.ExecQuery _
    ("Select Name from Win32_Process")
 
For Each oProcess In colProcesses
	sProcess = oProcess.Name
	If InStr(UCase(sProcess), UCase(sName)) Then
		bDetected = True
		sRes = oWS.Popup( "Detected active " & sProcess & " process..." & vbCrLf & vbCrLf &_
		"Would you like to terminate " & sProcess & " process?? ", ,cTitle ,vbInformation+vbyesno)
		If sRes = vbYes Then			
			oProcess.Terminate			
		End If		
	End If
Next
 
If Not bDetected Then
	oWS.Popup "No active processes detected that contain " & "''" & sName & "''", ,cTitle ,vbInformation
End If

Open in new window

0
 

Author Comment

by:ankur3020
ID: 22656069
Thanks si_shamil and hubasan.code is working perfectly. thanks a lot.
0
 

Author Comment

by:ankur3020
ID: 22656074
i hope the same code could work for windows nt /server also?
0
 
LVL 5

Expert Comment

by:si_shamil
ID: 22656808
I didn't check it against Windows NT.
I know that you will need to install WMI manually on windows NT, also make sure you have a latest version of WSH (5.6)
0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22658856
Yes, si_shamil is right,

On Windows NT you need to install Windows Management Instrumentation (WMI v1.5) and Windows Scripting Host (WSH v5.6) manually by downloading it from Microsoft's site.
0
 

Author Closing Comment

by:ankur3020
ID: 31502832
great job done.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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