[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3928
  • Last Modified:

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();

}



 

0
PeterSchenk
Asked:
PeterSchenk
  • 6
  • 4
1 Solution
 
käµfm³d 👽Commented:
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

0
 
PeterSchenkAuthor Commented:
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?
0
 
käµfm³d 👽Commented:
Sure, try:

using System.Diagnostics;

...

ProcessStartInfo info = new ProcessStartInfo("cmd", "/k 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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterSchenkAuthor Commented:
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>
0
 
käµfm³d 👽Commented:
Are the username and password you are sending valid?
0
 
PeterSchenkAuthor Commented:
Yes the are valid on the machine im trying to connect too.
0
 
PeterSchenkAuthor Commented:
The ip address is the target machine and the MACHINE98919 is my machine.
0
 
PeterSchenkAuthor Commented:
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 : )
0
 
PeterSchenkAuthor Commented:
This solution gave me the idea about the incorrect syntax...

Thanks you
0
 
käµfm³d 👽Commented:
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

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now