dmj12508 dmj12508 - 1 month ago 20
Vb.net Question

kill remote process

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)

Answer

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.