How to pass parameters to an exe?
Asked Answered
L

4

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 ?

Latecomer answered 19/4, 2015 at 10:56 Comment(5)
Wrap the arguments in "'s to avoid space issues. Arguments is the way to go, so you're close.Salvucci
@Salvucci 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.Latecomer
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.Salvucci
@Latecomer how would you type it into a command line window, or at the "start -> run" prompt? Try that first and translate that.Alchemy
@Alchemy 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.Latecomer
T
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]);
}
Thyme answered 19/4, 2015 at 11:22 Comment(0)
W
2

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

Winnow answered 18/7, 2017 at 8:21 Comment(0)
L
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].

Lenes answered 19/4, 2015 at 11:13 Comment(1)
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.Latecomer
U
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

Underbody answered 9/9, 2021 at 8:44 Comment(1)
Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.Inland

© 2022 - 2025 — McMap. All rights reserved.