Syntax Error ??

I have this code :

Dim strProcess
strProcess = "iexplore.exe"
strComputer = "."

  Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
    Set colProcesses = objWMIService.ExecQuery _
        ("SELECT * FROM Win32_Process WHERE Name = '" & strProcess & "'")

For Each objProcess In colProcessList
If objProcess.Name = strProcess Then
    objProcess.Terminate()                           '<----- Errors out here
MsgBox "Found Process : " & objProcess.Name

End If
Next

It errors out on the

  objProcess.Terminate()

Line.

I have microsoft wmi scripting v1.2 library and microsoft scripting runtime both checked and enabled in project -->references.

Also I found this example here from vbnet :

http://vbnet.mvps.org/index.html?code/wmi/wmi_processmonitor.htm

Was just wondering what I am doing wrong.
LVL 23
Shane Russell2nd Line Desktop SupportAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fostejoCommented:
gecko_au2003,

I think the issue is due to an glitch in the coding; the line refers to the wrong variable..

"For Each objProcess In colProcessList" needs changing to..

"For Each objProcess In colProcesses"

cheers
Shane Russell2nd Line Desktop SupportAuthor Commented:
Nope, changed that and it still comes up with the same error.

Also would it be better to use the vbnet example or the wmi I posted to monitor if you want to stop apps from launching on multiple remote machines ?
nffvrxqgrcfqvvcCommented:
I don't see any Variables declared... You need to declare them.

dim objProcess as variant
dim  colProcesses as object

Might work?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

nffvrxqgrcfqvvcCommented:
in regular VB this is working for me?


Option Explicit

Private Sub Command1_Click()
Dim strProcess As String
Dim strcomputer As String
Dim objWMIService As Object
Dim colProcesses As Object
Dim objProcess As Variant


strProcess = "iexplore.exe"
strcomputer = "."

  Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strcomputer & "\root\cimv2")
 
    Set colProcesses = objWMIService.ExecQuery _
        ("SELECT * FROM Win32_Process WHERE Name = '" & strProcess & "'")

For Each objProcess In colProcesses
If LCase$(objProcess.Name) = strProcess Then
    objProcess.Terminate
End If
Next

End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shane Russell2nd Line Desktop SupportAuthor Commented:
That worked ( will award you the points in a little bit ) but just curious which method out of the 2 I mentioned above would be better for stopping processes running on multiple machines on a network ?
nffvrxqgrcfqvvcCommented:
You should split points since fostejo had a valid point because that was also another error in the cod he pointed out.

I know that when I tested the code I didn't have any references set..it still worked without the references for me probrably because I am already running wmi scripting as a service but I belive that is standard with windows unless you stop it. if you use the reference I doubt you need to use getobject because that is still considered latebinding similiar to createobject. I could be wrong. I think the above would work but I can't test it over network
fostejoCommented:
Thanks gecko_au2003 & egl1004,

Worked fine for me without any additional references or declarations also..?

Also, I'd make just one other small mod to egl1044's code (just for completness really) - where the code is:

For Each objProcess In colProcesses
If LCase$(objProcess.Name) = strProcess Then
    objProcess.Terminate
End If

I'd alter it to

For Each objProcess In colProcesses
If LCase$(objProcess.Name) = LCase$(strProcess) Then
    objProcess.Terminate
End If

Just so the script is more generic (ie. Just in-case 'strProcess' is defined in the script in a mixture of upper/lowercase letters)

cheers!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.