dmj12508 dmj12508 - 3 months ago 38 Question

kill remote process

I used the below link and was able to set it up successfully:

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
Exit Sub
End If
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)


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)
  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.