views:

177

answers:

3

Im attempting to kill a process remotely by using pskill.

From the command line, pskill works great, but when trying the same command in C# Im getting an access denied error.

    var startInfo = new ProcessStartInfo {
                                            FileName = "pskill.exe",
                                            Arguments = "-t \\" + _currentMachine + 
                                                        " -u BobSmith -p Pass123 " + _currentService + 
                                                        " /acceptEULA"
                                         };

    try {
        using (Process exeProcess = Process.Start(startInfo)) {
            exeProcess.WaitForExit();
        }
    } catch {
        Console.WriteLine("Cannot forcibly kill process.");
    }

I have even tried creating a .bat file which runs fine manually but I get the same error when trying to call the .bat from C#.

Im on an XP machine attempting to access a win 2003 server.

A: 

I would double check your credentials. It is either that or your process is 'locked'.

drpcken
Assuming you are right, would you know why running the .bat file manually kills the process but not if I launch the file from within the C# code.
Coward
Looking at it again, how are you getting your _CurrentService. Have you tried getting PID instead?
drpcken
not yet but thats a great suggestion, Ill have to try that next.
Coward
I've always had better luck killing processes by the PID.
drpcken
A: 

In C# characters in strings are escaped by default. The string you assign to Arguments contains two escape characters. I suspect that they are turned into a single back slash. Add an @ in front of the string definition to resolve this.

  Arguments = @"-t \\" + _currentMachine + 

In any case I would suggest adding code to write the whole command line out to a text file and check that it is as you need it. If the text file has a bat extension can you run it successfully?

Philip Smith
A: 

For the next guy...

pskill.exe needed to be run in Win2000 compatibility mode.

Coward