How can i kill all instances of "Wscript.exe" exept one ?

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 10 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
hackoo
Posts: 103
Last visit: Tue Apr 26, 2016 9:02 am

How can i kill all instances of "Wscript.exe" exept one ?

Post by hackoo »

Hi ;)
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
VBScript Code
Double-click the code block to select all.
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 !
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How can i kill all instances of "Wscript.exe" exept one

Post by jvierra »

Just takes one simple loop and a filter.
VBScript Code
Double-click the code block to select all.
Set wmi = GetObject("winmgmts:root\cimv2")
Set cProcesses = wmi.ExecQuery("select * from win32_process where Name like '%cscript.exe%' AND NOT commandline like '%" & wsh.scriptname & "%'")

For Each oProcess in cProcesses
    wsh.echo oprocess.commandline
    oProcess.Terminate()
Next
That is all.

Change cscript.exe to wscript.exe if that is what you are usung - you shouldn't be using that.
User avatar
hackoo
Posts: 103
Last visit: Tue Apr 26, 2016 9:02 am

Re: How can i kill all instances of "Wscript.exe" exept one

Post by hackoo »

Thank you !
Works like a charm :D
This topic is 10 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked