Kill Process on Remote Machine

Private Sub ProcessKill(ByVal strIP As String, ByVal PID As Integer)

        Dim query As Management.ManagementObjectSearcher
        Dim queryCollection As Management.ManagementObjectCollection
        Dim management_object1 As Management.ManagementObject
        Dim msc As Management.ManagementScope

        Dim co As New Management.ConnectionOptions
        co.Username = UserName
        co.Password = Password

        If (strIP = Environment.MachineName.ToString) Then

            msc = New Management.ManagementScope("\\" & strIP & "\root\cimv2")
        Else
            msc = New Management.ManagementScope("\\" & strIP & "\root\cimv2", co)
        End If

        Dim id As String = PID
        Dim query_command As String = "SELECT * FROM Win32_Process where ProcessId=" & id
        Dim select_query As Management.SelectQuery = New Management.SelectQuery(query_command)

        Try
            query = New Management.ManagementObjectSearcher(msc, select_query)
            queryCollection = query.Get()
            For Each management_object1 In queryCollection
                Dim argList As String() = New String() {String.Empty}
                If (management_object1("ProcessId") = id) Then
                    Dim obj As Object() = New Object() {0}
                    management_object1.InvokeMethod("Terminate", obj)
                End If
            Next management_object1

        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try

End Sub

Related Posts by Categories



Stumble This Fav This With Technorati Add To Del.icio.us Digg This Add To Reddit Add To Facebook Add To Yahoo

0 comments:

Post a Comment