Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 397
MrProgram Avatar asked May 01 '26 13:05

MrProgram


2 Answers

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);
like image 162
MrProgram Avatar answered May 03 '26 01:05

MrProgram


Running an interactive program such as notepad.exe doesn't always open a visible window on the target PC. Try opening Task Manager on the target PC while you run the code and see if notepad.exe appears in the list of running processes.

I'd suggest trying to run psexec from the command line first before attempting to call it via code.

psexec \\serverName "notepad.exe"

You may also want to use the "interactive" flag so it can interact with the desktop on the target.

psexec \\serverName "notepad.exe" -i
like image 30
Peter Evans Avatar answered May 03 '26 03:05

Peter Evans