473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Nov 2 '07 #1
10 8042
Plater
7,872 Expert 4TB
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
797 Expert 512MB
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
797 Expert 512MB
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
7,872 Expert 4TB
I suspected that WMI would work (thus my RPC reference) but really had no hard proof of it.
Nov 2 '07 #5
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
7,872 Expert 4TB
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
797 Expert 512MB
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
uwesch
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
797 Expert 512MB
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
uwesch
2
Thank you!

Everything is working fine.

Sincerely,
Uwe
Dec 18 '07 #11

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

Similar topics

10
by: Vilmar Brazão de Oliveira | last post by:
HI people, what is wrong in the code bellow to kill iexplore.exe process after processing my page?? '»»Before comes routines to access data base and to send email. Set objWshell =...
10
by: Fred | last post by:
There is a setting in INIT.ORA that has the unintended side-effect of making sure the ALTER SYSTEM KILL SESSION command has immediate affect. Without this setting, I've seen some instances where...
0
by: Markus Poehler | last post by:
Hi my program should run on terminal server. I open Acrobat process and I have to kill them at some points in my application. This fails cause of insufficient rights on terminal server. the...
10
by: Jako Menkveld | last post by:
I'm building a relatively simple client-server app. One of the functions of the client is to notify the server when it terminates, this all works fine. The problem comes in when the server is...
4
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times....
1
by: Manfred Braun | last post by:
Hi All, I am writing a tool, which should monitor some exe-processes, which are not very solid. Th main function is to re-start them, if they hung, but this is complicated. I can detect things...
4
by: Steve B. | last post by:
I think a user exited my local application incorrectly and now I can't compile a new version of the application to the public network without getting a "in use" error message. Also, can't rename...
1
by: Lee Gillie | last post by:
I need to be able to kill a process programmatically under ASP.NET. IIS runs under the local system account. The user accessing the web is using anonymous access. We would prefer NOT to grant...
1
shrek123
by: shrek123 | last post by:
I want to kill some process on remote machine which is on some other domain. I am using Win32::OLE GetObject to do that. But I am getting following error Error1: When my remote machine is on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.