使用VB.Net中的WMI监听远程服务器上的进程

时间:2014-03-01 01:28:56

标签: vb.net wmi

编程语言:VB.Net 相关:WMI

我花了好几个小时来解决这个问题,但我无法让它发挥作用。 我可以在服务器上成功启动.bat文件,但是当我尝试创建一个观察者时 听它的DeletionEvent什么都没发生。我确实得到了一个ProcessID,因为我可以打印它的值。

非常感谢任何帮助!

我刚看到Catch语句以“Quota limit”执行。这怎么可能?我只是听一个进程(带有我的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

这是我在你的建议之后改变代码的方式:

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

我仍然可以获得配额限制异常:/

我没有在本地尝试过,.bat文件的性质只能在服务器上运行。

0 个答案:

没有答案