• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 915
  • Last Modified:

block /terminate process

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
ankur3020
Asked:
ankur3020
  • 5
  • 4
  • 3
3 Solutions
 
HubasanCommented:
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
 
HubasanCommented:
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
 
ankur3020Author Commented:
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!

 
si_shamilCommented:
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
 
si_shamilCommented:
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
 
HubasanCommented:
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
 
HubasanCommented:
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
 
ankur3020Author Commented:
Thanks si_shamil and hubasan.code is working perfectly. thanks a lot.
0
 
ankur3020Author Commented:
i hope the same code could work for windows nt /server also?
0
 
si_shamilCommented:
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
 
HubasanCommented:
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
 
ankur3020Author Commented:
great job done.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now