kill remote process

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 21:49:30

问题


I used the below link and was able to set it up successfully: http://www.codeproject.com/Articles/26817/Killing-Processes-from-a-Visual-Basic-Application?msg=4868229#xx4868229xx

I am trying to figure out how to view/kill processes for a remote workstation in VB.NET versus locally? Any ideas?

I've been testing with WMI but cant seem to figure it out and get it working.

Here is my class code:

Public Class Form1
    Public Sub endprocess(ByVal RemotePC As String, ByVal process As String)
        Dim objWMIService As Object
        Dim colProcessList As Array
        Dim objprocess As Object
        Dim response As Boolean
        Dim pcname As String = tbRemoteIP.Text

        objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & pcname & "\root\cimv2")
        process = "'" & process & "'"
        colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = " & "SDM Offline Tool")
        For Each objprocess In colProcessList
            response = MsgBox("End " & process & " on " & pcname & "?", MsgBoxStyle.YesNoCancel)
            If response = vbYes Then
                objprocess.Terminate()
            Else
                Exit Sub
            End If
        Next
    End Sub

I call the above with the below button:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    endprocess("pcname", "SDM Offline Tool")
End Sub

I'm getting a CAST error when i tried and end the SDM Offline Tool process on the remotepc (pcname)


回答1:


Try this code, it worked for me with "notepad.exe":

Public Sub endprocess(ipAddress As String, processName As String)
  Dim scope As New ManagementScope("\\" & ipAddress & "\root\cimv2")
  Dim query As New SelectQuery(
    "SELECT * FROM Win32_Process WHERE Name = '" & processName & "'")

  Using searcher As New ManagementObjectSearcher(scope, query)
    Dim queryCollection As ManagementObjectCollection = searcher.[Get]()
    For Each process As ManagementObject In queryCollection
      process.InvokeMethod("Terminate", Nothing)
    Next
  End Using
End Sub

Sub Main()
  endprocess("ipAddress", "notepad.exe")
End Sub

Don't forget to add reference to System.Management (.NET 4.0), and add Imports at the top.



来源:https://stackoverflow.com/questions/24941139/kill-remote-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!