Solved

block /terminate process

Posted on 2008-10-03
12
887 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
  • 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 75 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
 
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 50 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 7

Assisted Solution

by:Hubasan
Hubasan earned 75 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now