By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

Kill process on remote server

P: 1
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
Nov 2 '07 #1
Share this Question
Share on Google+
10 Replies


Plater
Expert 5K+
P: 7,872
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.
Nov 2 '07 #2

balabaster
Expert 100+
P: 797
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.
Nov 2 '07 #3

balabaster
Expert 100+
P: 797
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. 'Get the information
  3. Dim ComputerName As String = Console.ReadLine()
  4. Dim oConnectionOptions As New System.Management.ConnectionOptions()
  5. With oConnectionOptions
  6.     .Username = Console.ReadLine()
  7.     .Password = Console.ReadLine()
  8. End With
  9. Dim ProcessID As Integer = Console.ReadLine 'Obviously you'll need to know this ahead of time...
  10. 'Do the work
  11. Dim oPath As New ManagementPath("\\" & ComputerName & "\root\cimv2")
  12. Dim oScope As New ManagementScope(oPath, oConnectionOptions)
  13. oScope.Connect()
  14. If oScope.IsConnected Then
  15.     Dim oQuery As New ObjectQuery("Select * From Win32_Process Where ProcessID = " & ProcessID)
  16.     Dim oSearcher As New System.Management.ManagementObjectSearcher(oScope, oQuery)
  17.     For Each oManagementObject As ManagementObject In oSearcher.Get()
  18.         Dim oTerm(2) As Object
  19.         oManagementObject.InvokeMethod("Terminate", oTerm)
  20.     Next
  21.     oSearcher.Dispose()
  22. End If
  23.  
C#
Expand|Select|Wrap|Line Numbers
  1. //Get the information 
  2. string ComputerName = Console.ReadLine(); 
  3. System.Management.ConnectionOptions oConnectionOptions = new System.Management.ConnectionOptions(); 
  4.     oConnectionOptions.Username = Console.ReadLine(); 
  5.     oConnectionOptions.Password = Console.ReadLine(); 
  6. int ProcessID = Console.ReadLine; //Obviously you'll need to know this ahead of time... 
  7.  
  8. //Do the work 
  9. ManagementPath oPath = new ManagementPath("\\\\" + ComputerName + "\\root\\cimv2"); 
  10. ManagementScope oScope = new ManagementScope(oPath, oConnectionOptions); 
  11. oScope.Connect(); 
  12. if (oScope.IsConnected) { 
  13.     ObjectQuery oQuery = new ObjectQuery("Select * From Win32_Process Where ProcessID = " + ProcessID); 
  14.     System.Management.ManagementObjectSearcher oSearcher = new System.Management.ManagementObjectSearcher(oScope, oQuery); 
  15.     foreach (ManagementObject oManagementObject in oSearcher.Get()) { 
  16.         object[] oTerm = new object[2]; 
  17.         oManagementObject.InvokeMethod("Terminate", oTerm); 
  18.     } 
  19.     oSearcher.Dispose(); 
  20. }
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 oManagementObject.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.
Nov 2 '07 #4

Plater
Expert 5K+
P: 7,872
I suspected that WMI would work (thus my RPC reference) but really had no hard proof of it.
Nov 2 '07 #5

P: 9
you can use Win32 API after getting a list of processes from
Process.GetProcesses

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
Nov 5 '07 #6

Plater
Expert 5K+
P: 7,872
you can use Win32 API after getting a list of processes from
Process.GetProcesses
Does this work for remote machines as well?
Nov 5 '07 #7

balabaster
Expert 100+
P: 797
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.
Nov 5 '07 #8

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1. Try
  2.  
  3.             Dim classInstance As New ManagementObject( _
  4.                                     "\\" & strServer & "\root\CIMV2", _
  5.                                     "Win32_Process.Handle='" & ProcID & "'", Nothing)
  6.  
  7.             ' Obtain [in] parameters for the method
  8.             Dim inParams As ManagementBaseObject = _
  9.                 classInstance.GetMethodParameters("Terminate")
  10.  
  11.             ' Add the input parameters.
  12.  
  13.             ' Execute the method and obtain the return values.
  14.             Dim outParams As ManagementBaseObject = _
  15.                 classInstance.InvokeMethod("Terminate", inParams, Nothing)
  16.  
  17.         Catch err As ManagementException
  18.  
  19.             MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
  20.         End Try
  21.  
I get an COMException too on line:

Dim outParams As ManagementBaseObject = _
classInstance.InvokeMethod("Terminate", inParams, Nothing)

Can you help me out?

SIncerely,
Uwe
Nov 29 '07 #9

balabaster
Expert 100+
P: 797
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
Expand|Select|Wrap|Line Numbers
  1. Dim oProc As System.Diagnostics.Process = System.Diagnostics.Process.Start("c:\windows\notepad.exe")
  2.         Dim strServer As String = "."
  3.         Console.ReadLine()
  4.         Try
  5.             Dim classInstance As New ManagementObject( _
  6.                                     "\\" & strServer & "\root\CIMV2", _
  7.                                     "Win32_Process.Handle='" & oProc.Id & "'", Nothing)
  8.             ' Obtain [in] parameters for the method
  9.             Dim inParams As ManagementBaseObject = _
  10.                 classInstance.GetMethodParameters("Terminate")
  11.             ' Add the input parameters.
  12.             ' Execute the method and obtain the return values.
  13.             Dim outParams As ManagementBaseObject = _
  14.                 classInstance.InvokeMethod("Terminate", inParams, Nothing)
  15.         Catch err As ManagementException
  16.             Console.WriteLine("An error occurred while trying to execute the WMI method: " & err.Message)
  17.         End Try
  18.         Console.ReadLine()
Dec 18 '07 #10

P: 2
Thank you!

Everything is working fine.

Sincerely,
Uwe
Dec 18 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.