Link to home
Create AccountLog in
Avatar of Indarnav
Indarnav

asked on

check my code

i wrote one code, but getting error while running. error is : "SWbemObjectEx: Not found" at line 157.
set ows = createobject ("wscript.shell")
x= 0
do while i =0
sname = "explorer"
sname1 = "jusched"
sname5 = "acrotray"
sname6=  "alg"
sname7 = "svchost"
sname8 = "system idle process"
sname9 = "system"
sname13 = "smss"
sname14 = "csrss"
sname15 = "winlogon"
sname16 = "services"
sname17 = "lsass"
sname18 = "cfsmsmd"
sname19 = "cnfagent"
sname20 = "spoolsv"
sname21 = "cam"
sname22 = "mdm"
sname23 = "NTRtscan"
sname24 = "Ofant"
sname25 = "Tmlisten" 'left
sname26 = "sched"
sname27 = "PccNTUpd"
sname28 = "cfnotsrvd"
sname29 = "ccsmagtd"
sname30 = "cfFTPlungin"
sname31 = "taskmgr"
sname32 = "wininit"
sname33 = "audiodg"
sname34 = "wmiprvse"
sname35 = "lsm"
sname36 = "ccsvchst"
 
 
 
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
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName1)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName5)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName6)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName7)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName8)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName9)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName13)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName14)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName15)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName16)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName17)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName18)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName19)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName20)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName21)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName22)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName23)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName24)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName25)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName26)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName27)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName28)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName29)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName30)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName31)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName32)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName33)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName34)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName35)) then
x = x +1
elseif instr(Ucase(sprocess), Ucase(sName36)) then
x = x +1
 
 
x = 0
else 
oprocess.terminate
end If
 
next
wscript.sleep 12000
Loop

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Aard Vark
Aard Vark
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Might as well write it with the input file as well to see if that might be of assistance too.
Just put the Process names into a text file in whatever location and point the script sFilePath variable to it to read the process names into the dictionary.
sComputer = "."
sFilePath = "C:\temp\processes.txt"
x = 0
 
Set ows = CreateObject ("WScript.Shell")
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(sFilePath,1,False)
 
Do Until objFile.AtEndOfStream
	sTemp = objFile.ReadLine
	If Not objDictionary.Exists(sTemp) Then objDictionary.Add UCASE(sTemp),""
Loop
 
objFile.Close
 
Do While i =0
	Set oWMIService = GetObject("winmgmts:\\" & scomputer & "\root\cimv2")
	Set colProcesses = oWMIService.ExecQuery("SELECT Name FROM Win32_Process")
	 
	For Each oProcess In colProcesses
		With oProcess
			If objDictionary.Exists(UCASE(.Name)) Then
				x = x + 1
			Else
				.Terminate
			End If
		End With
	Next
	x = 0
	WScript.Sleep 12000
Loop

Open in new window

Avatar of Indarnav
Indarnav

ASKER

tested ur dictionary code, getting error in line 53, SWbemObjectEx: Not found...

please recheck code,
re checked your disctionay code on xp, it worked but only once. loop not working.. surprised why it did not worked on vista properly although i added other required files.  please review closely and guide me.
Not sure I've run it on XP and Vista now and it's running OK. Loops every 12 seconds and kills off any processes not listed.
i retested , code is running in vista too. but it exits after one run. not running in loop.. just have a look of what i m working on. and incase any change is required please add.

more strange is loop is not working in vista nor in xp.
sComputer = "."
x = 0
 
Set ows = CreateObject ("WScript.Shell")
Set objDictionary = CreateObject("Scripting.Dictionary")
 
If Not objDictionary.Exists("explorer.exe") Then objDictionary.Add UCASE("explorer.exe"),""
If Not objDictionary.Exists("jusched.exe") Then objDictionary.Add UCASE("jusched.exe"),""
If Not objDictionary.Exists("acrotray.exe") Then objDictionary.Add UCASE("acrotray.exe"),""
If Not objDictionary.Exists("alg.exe") Then objDictionary.Add UCASE("alg.exe"),""
If Not objDictionary.Exists("svchost.exe") Then objDictionary.Add UCASE("svchost.exe"),""
If Not objDictionary.Exists("system idle process") Then objDictionary.Add UCASE("system idle process"),""
If Not objDictionary.Exists("system") Then objDictionary.Add UCASE("system"),""
If Not objDictionary.Exists("smss.exe") Then objDictionary.Add UCASE("smss.exe"),""
If Not objDictionary.Exists("csrss.exe") Then objDictionary.Add UCASE("csrss.exe"),""
If Not objDictionary.Exists("winlogon.exe") Then objDictionary.Add UCASE("winlogon.exe"),""
If Not objDictionary.Exists("services.exe") Then objDictionary.Add UCASE("services.exe"),""
If Not objDictionary.Exists("lsass.exe") Then objDictionary.Add UCASE("lsass.exe"),""
If Not objDictionary.Exists("cfsmsmd.exe") Then objDictionary.Add UCASE("cfsmsmd.exe"),""
If Not objDictionary.Exists("cnfagent.exe") Then objDictionary.Add UCASE("cnfagent.exe"),""
If Not objDictionary.Exists("spoolsv.exe") Then objDictionary.Add UCASE("spoolsv.exe"),""
If Not objDictionary.Exists("cam.exe") Then objDictionary.Add UCASE("cam.exe"),""
If Not objDictionary.Exists("mdm.exe") Then objDictionary.Add UCASE("mdm.exe"),""
If Not objDictionary.Exists("NTRtscan.exe") Then objDictionary.Add UCASE("NTRtscan.exe"),""
If Not objDictionary.Exists("Ofant.exe") Then objDictionary.Add UCASE("Ofant.exe"),""
If Not objDictionary.Exists("Tmlisten.exe") Then objDictionary.Add UCASE("Tmlisten.exe"),""
If Not objDictionary.Exists("sched.exe") Then objDictionary.Add UCASE("sched.exe"),""
If Not objDictionary.Exists("PccNTUpd.exe") Then objDictionary.Add UCASE("PccNTUpd.exe"),""
If Not objDictionary.Exists("cfnotsrvd.exe") Then objDictionary.Add UCASE("cfnotsrvd.exe"),""
If Not objDictionary.Exists("ccsmagtd.exe") Then objDictionary.Add UCASE("ccsmagtd.exe"),""
If Not objDictionary.Exists("cfFTPlungin.exe") Then objDictionary.Add UCASE("cfFTPlungin.exe"),""
If Not objDictionary.Exists("taskmgr.exe") Then objDictionary.Add UCASE("taskmgr.exe"),""
If Not objDictionary.Exists("wininit.exe") Then objDictionary.Add UCASE("wininit.exe"),""
If Not objDictionary.Exists("audiodg.exe") Then objDictionary.Add UCASE("audiodg.exe"),""
If Not objDictionary.Exists("wmiprvse.exe") Then objDictionary.Add UCASE("wmiprvse.exe"),""
If Not objDictionary.Exists("lsm.exe") Then objDictionary.Add UCASE("lsm.exe"),""
If Not objDictionary.Exists("ccsvchst.exe") Then objDictionary.Add UCASE("ccsvchst.exe"),""
If Not objDictionary.Exists("smsvchost.exe") Then objDictionary.Add UCASE("smsvchost.exe"),""
If Not objDictionary.Exists("wudfhost.exe") Then objDictionary.Add UCASE("wudfhost.exe"),""
If Not objDictionary.Exists("unsecapp.exe") Then objDictionary.Add UCASE("unsecapp.exe"),""
If Not objDictionary.Exists("dwm.exe") Then objDictionary.Add UCASE("dwm.exe"),""
If Not objDictionary.Exists("sistray.exe") Then objDictionary.Add UCASE("sistray.exe"),""
If Not objDictionary.Exists("sidebar.exe") Then objDictionary.Add UCASE("sidebar.exe"),""
If Not objDictionary.Exists("trustedinstaller.exe") Then objDictionary.Add UCASE("trustedinsatller.exe"),""
 
 
 
 
Do While i =0
	Set oWMIService = GetObject("winmgmts:\\" & scomputer & "\root\cimv2")
	Set colProcesses = oWMIService.ExecQuery("SELECT Name FROM Win32_Process")
	 
	For Each oProcess In colProcesses
		With oProcess
			If objDictionary.Exists(UCASE(.Name)) Then
				x = x + 1
			Else
				.Terminate
			End If
		End With
	Next
	x = 0
	WScript.Sleep 12000
Loop

Open in new window

i added wscript and cscript.exe too, now when i am running by double clicking  on vbs file, it is running in loop. but when i made exe of that with scriptcrypto , that is agian not running in loop. any other better converter
it worked. thanks
i made exe of it, when  i m running that, it opens one console with path of exe at console title. how to hide it? why console is coming.