Remotely ending a process

delstar

Well-known member
Joined
Jun 9, 2006
Messages
59
Programming Experience
1-3
I know that there's a way to do it through WMI, just can't seem to find how to use it. Anyone able to help?
 
There may be other ways, but here's how I do it:

VB.NET:
    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

        objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & RemotePC & "\root\cimv2")
        process = "'" & process & "'"
        colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = " & process)
        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

Alternatively you could download the PSTools suite from sysinternals and use pskill.exe to kill the process.
 
This is the way to kill all Notepad processes if found:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] notepadProcs() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName([/SIZE][SIZE=2][COLOR=#800000]"Notepad"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"."[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#008000]'The character (.) equals to this machine's name
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] proc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Diagnostics.Process [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] notepadProcs
proc.Kill()
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]
 
This is the way to kill all Notepad processes if found:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] notepadProcs() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName([/SIZE][SIZE=2][COLOR=#800000]"Notepad"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"."[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#008000]'The character (.) equals to this machine's name
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] proc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Diagnostics.Process [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] notepadProcs
proc.Kill()
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]

proc.kill() will only kill a process on the local machine. There's no way to pass the machinename to Kill()
 
You can change the dot in this line to a machine name to kill Notepad remotely:
System.Diagnostics.Process.GetProcessesByName("Notepad", ".")

GetProcessesByName() doesn't kill the process, just retrieves a list of the processes (or, in the case of this overload, checks to see if the process is running). After a bit of research, it seems to me the only way to remotely kill a process is through WMI. I was hoping to avoid using WMI, but it seems necessary.
 
GetProcesses.. will get you an array with references to these Process instances, but for remote process it can't be used to Kill, that is true.
If you struggle with WMI you haven't yet discovered WMI Code Creator.
 
Back
Top