jPol34
jPol34

Reputation: 61

Why is my application failing when launched remotely?

I have a Windows application that sits on four different servers, its purpose is to look through a directory, find .wav files and copy them to a remote location. It works perfectly when ran from directory it's in on the server, but I don't like going to four different servers to execute the application. So below, I have a console app that is really just telling the user to store the IDs for the wav files in the right location so the four applications can see them, then executes the various programs. The problem is all four apps crash immediately after launch, I think it has something to do with how I'm executing the command, but I can't figure it out. Any ideas?

class Program
{
    static void Main()
    {
        Console.SetWindowSize(90, 35);
        Console.WriteLine(@"Place the ID of the interaction(s) you wish to recover in \\SERVER\c$\IDKeys.txt." + Environment.NewLine);
        Console.WriteLine("Press Enter to find interactions.");
        Console.ReadLine();
        exeCmd();       
    }

    static void exeCmd()
    {
        string[] executable =
        {
            @"\\Server1\C$\App\findwavs.exe",
            @"\\Server2\C$\App\findwavs.exe",
            @"\\Server3\C$\App\findwavs.exe",
            @"\\Server4\C$\App\findwavs.exe"
        };

        foreach (string exe in executable)
        {
            Process proc = new Process();
            proc.StartInfo.FileName = exe;
            proc.Start();
        }
    }
}

Upvotes: 0

Views: 328

Answers (2)

Yacoub Massad
Yacoub Massad

Reputation: 27861

The code you have executes the executable on the local machine.

Using @"\\Server1\C$\App\findwavs.exe does not mean that the executable will run on Server1, it just means that your local machine will look in this network path to obtain the executable file, but will run it locally.

You can use PsExec to execute an application on a remote machine.

Consider the following code that uses PsExec to run an executable on a remote machine:

string application = "c:\\app\\findwavs.exe";
string arguments = ""; //use this if you want to pass any command line arguments to the findwavs.exe executable
string location_of_psexec = "c:\\pstools\\psexec.exe"; //location of Psexec.exe on local machine
string remote_machine = "Server1"; //name of remote machine

Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = location_of_psexec;
process.StartInfo.Arguments = string.Format("\\\\{0} -c \"{1}\" \"{2}\"", remote_machine, application, arguments);

This code assumes that the application path (e.g. "c:\app\findwavs.exe") is relative to your local machine, not the remote machine.

If you want to use a path relative to the remote machine, simply remove the -c from the value of process.StartInfo.Arguments

UPDATE:

Quoting from the above reference:

If you omit a user name, the process will run in the context of your account on the remote system, but will not have access to network resources (because it is impersonating). Specify a valid user name in the Domain\User syntax if the remote process requires access to network resources or to run in a different account. Note that the password and command are encrypted in transit to the remote system.

To make you application able to access network resources, use the -u and -p switches to supply a username and a password.

Upvotes: 4

Lews Therin
Lews Therin

Reputation: 3777

Running your code would be equivalent to navigating to \\Server1\C$\App\findwavs.exe in Explorer and double clicking on the exe. What I mean by this is that it will run the program on the machine that told it to run, not where the exe is stored.

The easiest way I can see to accomplish the task you want to perform would be to use PsExec.

Upvotes: 3

Related Questions