9

I am using psexec on my server to run an exe file on another server. How do I pass parameters to the other exe ?

The exe that I am running on my server is psexec which in turn must run the exe named vmtoolsd.exe located on another system. How do I pass parameters to vmtoolsd.exe ? Also, where do I pass it ? Would I pass it as part of the info.Arguments ? I've tried that but it isn't working.

ProcessStartInfo info = new ProcessStartInfo(@"C:\Tools");
info.FileName = @"C:\Tools\psexec.exe";
info.Arguments = @"\\" + serverIP + @"C:\Program Files\VMware\VMwareTools\vmtoolsd.exe";
Process.Start(info);

Also, as part of info.Arguments would I have to preface the path of vmtoolsd.exe with the IP address, followed by the drive path ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
spdcbr
  • 305
  • 3
  • 5
  • 15
  • 1
    Wrap the arguments in `"`'s to avoid space issues. `Arguments` is the way to go, so you're close. – SimpleVar Apr 19 '15 at 11:00
  • @YoryeNathan Should I place the arguments right after vmtools.exe ? Or should I do something like + "arguments to the exe" ? I've tried "info.Arguments = @"""\\" + serverIP + @"\C:\Program Files\VMware\VMware Tools\vmtoolsd.exe ""--cmd ""info-get guestinfo.test""" ; , but I get an error. It takes the arguments as part of the path. It says couldn't access the path. – spdcbr Apr 19 '15 at 11:07
  • I'm not familiar with `psexec`. Find out what arguments you want to send, and send them wrapped in `"`'s, delimited by space. You have two problems: sending proper arguments, and sending those proper arguments via C#. Solve the 1st problem first. – SimpleVar Apr 19 '15 at 11:09
  • 1
    @spdcbr how would you type it into a command line window, or at the "start -> run" prompt? Try that first and translate that. – Alex Apr 19 '15 at 11:10
  • @Alex This is the command that I type on the server where vmtoolsd.exe is located: "C:\Program Files\VMware\VMware Tools\vmtoolsd.exe" --cmd "info-get guestinfo.test". I tried translating it, but I'm not sure where I'm going wrong. The first path I've specified as part of the arguments. I'm not sure how the add the second part of it. – spdcbr Apr 19 '15 at 11:13

4 Answers4

27

Hope the below code may help.

Code from first .exe:

Process p= new Process();
p.StartInfo.FileName = "demo.exe";
p.StartInfo.Arguments = "param1 param2";
p.Start();
p.WaitForExit();

or

Process.Start("demo.exe", "param1 param2");

Code in demo.exe:

static void Main (string [] args)
{
  Console.WriteLine(args[0]);
  Console.WriteLine(args[1]);
}
Blue
  • 22,608
  • 7
  • 62
  • 92
Mou
  • 15,673
  • 43
  • 156
  • 275
2

Right click on .exe file-->goto shortcut-->in target tab write the arguement in extreme right... in my case it worked

Brijesh Ray
  • 779
  • 8
  • 15
0

You can see it in the following post (answer by @AndyMcCluggage):

How do I start a process from C#?

using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();// Waits here for the process to exit.

It provides far more control as you can see in the MSDN, but basically the arguments control is quite easy as you can see, is just to modify a property with a string.

Update: Since with the snippet code above you would be starting PsExec, based on:

PsExec

The format you have to use is:

psexec @run_file [options] command [arguments]

Where: arguments Arguments to pass (file paths must be absolute paths on the target system).

Since the process you're starting is psexec, in the process.StartInfo.Arguments you would have to put all the parameters it would need, in a sigle chain: @run_file [options] command [arguments].

Community
  • 1
  • 1
Btc Sources
  • 1,912
  • 2
  • 30
  • 58
  • In the above code snippet, the arguments will be to the exe on my server. I need to pass arguments to an exe on another server which my exe will start. – spdcbr Apr 19 '15 at 11:17
0

Step1.Create Shortcut and then Right click on Shortcut-->click properties page then target tab write the comment line argument in extreme right... this way worked for me

  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 09 '21 at 09:10