Question

Programming language: VB.Net Related to: WMI

I've spent hours on this problem but I just can't get it to work. I can successfully start a .bat file on the server, but when I try to create a watcher and listen for its DeletionEvent nothing happens. I do get a ProcessID back as I can print its value if I want.

Extremely grateful for any help!

I just saw that the Catch statement executes with "Quota limit". By how can this be? I just listen for one process (the one with my ProcessID)?

Public Sub Execute()

    Try
        Dim options As New System.Management.ConnectionOptions
        options.Username = strUser
        options.Password = strPassword

        Dim path As New System.Management.ManagementPath("\\" + strServer + "\root\CIMV2:Win32_Process")
        Dim scope As New System.Management.ManagementScope(path, options)
        Dim opt As New System.Management.ObjectGetOptions()
        Dim classInstance As New System.Management.ManagementClass(scope, path, opt)

        Dim inParams As System.Management.ManagementBaseObject = classInstance.GetMethodParameters("Create")
        inParams("CommandLine") = BuildString()

        ' Execute the method and obtain the return values.
        Dim outParams As System.Management.ManagementBaseObject = classInstance.InvokeMethod("Create", inParams, Nothing)
        Dim pid As Integer = outParams("processId")
        Dim query As New EventQuery
        'query.QueryString = "SELECT * FROM" & _
        '" __InstanceDeletionEvent WITHIN 1 " & _
        '"WHERE TargetInstance isa ""Win32_Process"""
        MsgBox("SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance.ProcessId = " & pid)

        ' Define query to select only our ProcessID
        query.QueryString = "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance.ProcessId = " & pid.ToString()

        ' Create ManagementEventWatcher with query
        Dim watcher As New ManagementEventWatcher(query)

        'Start listening for events matching the query and add Handler
        watcher.Start()
        AddHandler watcher.EventArrived, AddressOf HandleEvent

        ' Stop listening
        watcher.Stop()
    Catch err As Exception
        MsgBox("An error occured while trying to execute the WMI method: " & err.Message)
    End Try

End Sub

Private Sub HandleEvent(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
    MsgBox("Event occured!")
End Sub

This is how I changed the code after your suggestion:

'Start listening for events matching the query and add Handler
AddHandler watcher.EventArrived, AddressOf HandleEvent
watcher.Start()
watcher.WaitForNextEvent()

I still get the Quota limit exception though :/

I haven't tried it locally, the nature of the .bat file makes only runnable on the server.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top