Link to home
Start Free TrialLog in
Avatar of PeterSchenk
PeterSchenk

asked on

Using PSEXEC in C#

Hi,

Im using PSEXEC v1.98 and the following code to launch an application on a remote machine and Im having an issue.

When launching from a command prompt this code works fine...

psexec -i 1 -d -u Administrator -p Administrator \\28.228.176.111 notepad.exe

Now when I code this in C# it launches a command prompt localy loads PSEXEC then closes.... not sure where to go from here?

 

string stfamtv0001 = "/C" + " " + ("psexec -i 1 -d -u Administrator -p Administrator \\28.228.176.111 notepad.exe"

);

 



private void button1_Click(object sender, EventArgs

e)

{

PCdata(stfamtv0001);

}

 



private void PCdata(string

PCstreamPath)

{

System.Diagnostics.



Process

ps;

ps =



new System.Diagnostics.Process

();

ps.EnableRaisingEvents =



false

;

ps.StartInfo.UseShellExecute =



false

;

System.Diagnostics.



Process.Start("CMD.exe"

, PCstreamPath);

ps.Close();

}



 

Avatar of kaufmed
kaufmed
Flag of United States of America image

Try this:

using System.Diagnostics;

...

ProcessStartInfo info = new ProcessStartInfo("psexec", "-i 1 -d -u Administrator -p Administrator \\28.228.176.111 notepad.exe");

info.UseShellExecute = false;
Process p = Process.Start(info);

Open in new window

Avatar of PeterSchenk
PeterSchenk

ASKER

Did not work.... it flashes to quick to see whats happening.  Is there a way we can wrtie the output of the CMD to a txt file?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks like it trying to start it on the remote machine.... see below

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com


PsExec could not start \28.228.176.111 on MACHINE98919:
Logon failure: unknown user name or bad password.

c:\documents and settings\homeuser\my documents\visual studio 2010\Projects\Conso
leApplication2\ConsoleApplication2\bin\Debug>
Are the username and password you are sending valid?
Yes the are valid on the machine im trying to connect too.
The ip address is the target machine and the MACHINE98919 is my machine.
I actually fixed it with this command

string stfamtv0001 = "/C psexec -i 1 -d -u Administrator -p Administrator \\\\28.228.176.111 notepad.exe";

The \\28.228.176.111 was the issue... once it was changed to \\\\28.228.176.111 it worked : )
This solution gave me the idea about the incorrect syntax...

Thanks you
Ah yes. In C# if you have backslashes in a string, you either have to double them up, or prefix your string with the @ symbol. In other words:

string stfamtv0001 = "/C psexec -i 1 -d -u Administrator -p Administrator \\\\28.228.176.111 notepad.exe";

Open in new window


or

string stfamtv0001 = @"/C psexec -i 1 -d -u Administrator -p Administrator \\28.228.176.111 notepad.exe";

Open in new window