Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i kill all instances of "Wscript.exe" exept one that is started by my VBS ?

Status
Not open for further replies.

crackoo

Programmer
Feb 17, 2011
132
TN
Hi [peace]
Please i need some help to solve my issue !
so the question is : How can i kill all instances of "Wscript.exe" exept one that is started by my VBS ?
I made this script , but i get an error at line 19

Code:
Set oWMISrvc = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")

sProcName = Mid(wsh.fullname, InstrRev(wsh.fullname, "\") + 1)

Set cProcesses = oWMISrvc.ExecQuery( _
"select * from win32_process where Name = '" & sProcName & "'")

For Each oProcess in cProcesses
	If Instr(lcase(oProcess.Commandline), lcase(wsh.scriptname)) < 0 Then
		wsh.echo oProcess.Commandline
		wsh.echo wsh.scriptname
	else
		Process2kill = Mid(oProcess.CommandLine,InStr(oProcess.CommandLine,""" """) + 2)
		Process2kill = Replace(Process2kill,chr(34),"")
		msgbox Process2kill 
		Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
		& "where commandline like '" & Process2kill &"'",,48)
		For Each objItem in colItems 
			Wscript.Echo "Terminating script with this CommandLine: " & objItem.CommandLine
			objItem.Terminate()
		Next
	End If
Next
msgbox "ok"
Thank you !
 
You already have the process. Why re-query win32_process?

Code:
For Each oProcess in cProcesses
	If Instr(lcase(oProcess.Commandline), lcase(wsh.scriptname)) < 0 Then
		wsh.echo oProcess.Commandline
		wsh.echo wsh.scriptname
	else
		Wscript.Echo "Terminating script with this CommandLine: " & oProcess.CommandLine
		oProcess.Terminate()
	End If
Next

Although, if you have a reason to re-query, use the processID instead of commandLine.

Code:
Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
& "where processID = " & oProceess.processID,,48)
For Each objItem in colItems 
	Wscript.Echo "Terminating script with this CommandLine: " & objItem.CommandLine
	objItem.Terminate()
Next

-Geates


 
Hi [peace]
My Problem is solved like this :
Just takes one simple loop and a filter !

Code:
Set wmi = GetObject("winmgmts:root\cimv2") 
 Set cProcesses = wmi.ExecQuery("select * from win32_process where Name like '%wscript.exe%' AND NOT commandline like '%" & wsh.scriptname & "%'") 
  For Each oProcess in cProcesses 
     wsh.echo oprocess.commandline 
     oProcess.Terminate() 
 Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top