• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

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.
0
gecko_au2003
Asked:
gecko_au2003
  • 3
  • 2
  • 2
3 Solutions
 
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
0
 
gecko_au2003Author 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 ?
0
 
nffvrxqgrcfqvvcCommented:
I don't see any Variables declared... You need to declare them.

dim objProcess as variant
dim  colProcesses as object

Might work?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
0
 
gecko_au2003Author 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 ?
0
 
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
0
 
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!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now