Search code examples
c#remote-serverpsexec

Execute exe on remote machine


I'm trying to execute notepad.exe on a remote machine, but it's not working now. What am I missing?

var ui = new ImpersonateUser();
    //the process to restart
    const string processName = "notepad.exe";
    var serverName = "serverName";

    try
    {
        //Use adbadmin for access
        ui.Impersonate(_domain, _userName, _pass);

        //Start the process
        ProcessStartInfo info = new ProcessStartInfo("C:\\PsTools");
        info.FileName = @"C:\PsTools\psexec.exe";
        info.Arguments = @"""\\" + serverName + @"C:\WINDOWS\notepad.exe""";
        info.RedirectStandardOutput = true;
        info.UseShellExecute = false;
        Process p = Process.Start(info);

        lblStatusResponse.Text = "Service " + processName + " was restarted correctly.";
    }
    catch (Exception ex)
    {
        lblStatusResponse.Text = ex.ToString();
    }
    finally
    {
        ui.Undo();
    }

This gives me no exception, but I'm surely missing something...


Solution

  • The answer was a combination from your replies. But the whole correct solution was:

            ProcessStartInfo info = new ProcessStartInfo("C:\\PsTools");
            info.FileName = @"C:\PsTools\psexec.exe";
            info.Arguments = @"\\" + serverName + @" -i C:\WINDOWS\notepad.exe";
            info.RedirectStandardOutput = true;
            info.UseShellExecute = false;
            Process p = Process.Start(info);