Kill process on remote server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fadel daher
    New Member
    • Nov 2007
    • 1

    Kill process on remote server

    i am building an application to monitor servers.
    i have written code that gets me all running processes on remote server, but i cant manage to get a code to kill a remote process .
    can any one help me or provide me with a code that does the following:

    - kill a remote process ( i have already written a code to retreive all running processes on remote server)

    - restart a remote server

    and thank you for ur help
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    restarting a remote server can be done with the"shutdown" proccess.
    (check msdn for usage)

    As for killing a remote process, I'm not sure how to do that, unless there's like and RPC call that you can do. OR wirte some listener program that sits on each computer and waits for your special command kills off the desired process. But that's kind of a big security hubub.

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Originally posted by Plater
      restarting a remote server can be done with the"shutdown" proccess.
      (check msdn for usage)

      As for killing a remote process, I'm not sure how to do that, unless there's like and RPC call that you can do. OR wirte some listener program that sits on each computer and waits for your special command kills off the desired process. But that's kind of a big security hubub.
      This sounds like a prime candidate for WMI. The Windows Management Instrumentation allows you to go in behind the scenes and start/stop services, start/stop processes etc. I have the code to do this somewhere, I'll dig it out...and get back to you.

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by Plater
        restarting a remote server can be done with the"shutdown" proccess.
        (check msdn for usage)

        As for killing a remote process, I'm not sure how to do that, unless there's like and RPC call that you can do. OR wirte some listener program that sits on each computer and waits for your special command kills off the desired process. But that's kind of a big security hubub.
        VB
        Code:
         
        'Get the information
        Dim ComputerName As String = Console.ReadLine()
        Dim oConnectionOptions As New System.Management.ConnectionOptions()
        With oConnectionOptions
        	.Username = Console.ReadLine()
        	.Password = Console.ReadLine()
        End With
        Dim ProcessID As Integer = Console.ReadLine 'Obviously you'll need to know this ahead of time...
        'Do the work
        Dim oPath As New ManagementPath("\\" & ComputerName & "\root\cimv2")
        Dim oScope As New ManagementScope(oPath, oConnectionOptions)
        oScope.Connect()
        If oScope.IsConnected Then
        	Dim oQuery As New ObjectQuery("Select * From Win32_Process Where ProcessID = " & ProcessID)
        	Dim oSearcher As New System.Management.ManagementObjectSearcher(oScope, oQuery)
        	For Each oManagementObject As ManagementObject In oSearcher.Get()
        		Dim oTerm(2) As Object
        		oManagementObject.InvokeMethod("Terminate", oTerm)
        	Next
        	oSearcher.Dispose()
        End If
        C#
        Code:
        //Get the information 
        string ComputerName = Console.ReadLine(); 
        System.Management.ConnectionOptions oConnectionOptions = new System.Management.ConnectionOptions(); 
        { 
        	oConnectionOptions.Username = Console.ReadLine(); 
        	oConnectionOptions.Password = Console.ReadLine(); 
        } 
        int ProcessID = Console.ReadLine; //Obviously you'll need to know this ahead of time... 
        	
        //Do the work 
        ManagementPath oPath = new ManagementPath("[url=""]\\\\[/url]" + ComputerName + "[url="file://root//cimv2"]\\root\\cimv2[/url]"); 
        ManagementScope oScope = new ManagementScope(oPath, oConnectionOptions); 
        oScope.Connect(); 
        if (oScope.IsConnected) { 
        	ObjectQuery oQuery = new ObjectQuery("Select * From Win32_Process Where ProcessID = " + ProcessID); 
        	System.Management.ManagementObjectSearcher oSearcher = new System.Management.ManagementObjectSearcher(oScope, oQuery); 
        	foreach (ManagementObject oManagementObject in oSearcher.Get()) { 
        		object[] oTerm = new object[2]; 
        		oManagementObject.InvokeMethod("Terminate", oTerm); 
        	} 
        	oSearcher.Dispose(); 
        }
        Obviously you're not required to know the process id ahead of time - if you're doing it using the process name then you could change your query to "Select * From Win32_Process Where Name = "& ProcessName. In this manner you would loop through the collection (oSearcher.Get) and compare oManagementObje ct.Caption == ProcessName and if there is a match, then invoke the Terminate method in the same manner as we are doing here. I know our management gets a little antsy with us using RPC, but at least with WMI it checks security.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I suspected that WMI would work (thus my RPC reference) but really had no hard proof of it.

          Comment

          • hemendravyas
            New Member
            • Aug 2007
            • 9

            #6
            you can use Win32 API after getting a list of processes from
            Process.GetProc esses

            You need to have a PId and on passing of which to a Kill() witll kill the given process.

            You should have permissions on ther emote machine to do it.
            I'm able to do this same thing on my local machine........

            you can get back for any help at hvyas at irevna.com

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by hemendravyas
              you can use Win32 API after getting a list of processes from
              Process.GetProc esses
              Does this work for remote machines as well?

              Comment

              • balabaster
                Recognized Expert Contributor
                • Mar 2007
                • 798

                #8
                Originally posted by Plater
                Does this work for remote machines as well?
                The WMI option I presented does. However the Win32 Api call I'm not so sure about...I'm pretty sure that's only in a local context.

                Comment

                • uwesch
                  New Member
                  • Nov 2007
                  • 2

                  #9
                  Hi balabaster,

                  unfortunately your code isn't working properly in my program. If i start the script i get an COMExeption error.

                  So i used the program "WMI Code Creator" to get an working code, but the code isn't working too.

                  Here is the VB code i got:
                  Code:
                  Try
                  
                              Dim classInstance As New ManagementObject( _
                                                      "\\" & strServer & "\root\CIMV2", _
                                                      "Win32_Process.Handle='" & ProcID & "'", Nothing)
                  
                              ' Obtain [in] parameters for the method
                              Dim inParams As ManagementBaseObject = _
                                  classInstance.GetMethodParameters("Terminate")
                  
                              ' Add the input parameters.
                  
                              ' Execute the method and obtain the return values.
                              Dim outParams As ManagementBaseObject = _
                                  classInstance.InvokeMethod("Terminate", inParams, Nothing)
                  
                          Catch err As ManagementException
                  
                              MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
                          End Try
                  I get an COMException too on line:

                  Dim outParams As ManagementBaseO bject = _
                  classInstance.I nvokeMethod("Te rminate", inParams, Nothing)

                  Can you help me out?

                  SIncerely,
                  Uwe
                  Last edited by uwesch; Nov 29 '07, 11:58 AM. Reason: more information

                  Comment

                  • balabaster
                    Recognized Expert Contributor
                    • Mar 2007
                    • 798

                    #10
                    Sorry to say, I'm not able to replicate the issue...which leads me to the conclusion that it's the context in which you're implementing this block of code that's causing the issue.

                    This is what I've got in VB
                    Code:
                    Dim oProc As System.Diagnostics.Process = System.Diagnostics.Process.Start("c:\windows\notepad.exe")
                    		Dim strServer As String = "."
                    		Console.ReadLine()
                    		Try
                    			Dim classInstance As New ManagementObject( _
                    									"\\" & strServer & "\root\CIMV2", _
                    									"Win32_Process.Handle='" & oProc.Id & "'", Nothing)
                    			' Obtain [in] parameters for the method
                    			Dim inParams As ManagementBaseObject = _
                    				classInstance.GetMethodParameters("Terminate")
                    			' Add the input parameters.
                    			' Execute the method and obtain the return values.
                    			Dim outParams As ManagementBaseObject = _
                    				classInstance.InvokeMethod("Terminate", inParams, Nothing)
                    		Catch err As ManagementException
                    			Console.WriteLine("An error occurred while trying to execute the WMI method: " & err.Message)
                    		End Try
                    		Console.ReadLine()

                    Comment

                    • uwesch
                      New Member
                      • Nov 2007
                      • 2

                      #11
                      Thank you!

                      Everything is working fine.

                      Sincerely,
                      Uwe

                      Comment

                      Working...