简体   繁体   中英

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

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);

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

Your UNC path does not look good.

After the string concatenation, it will look something like

  "\\serverNameC:\WINDOWS\notepad.exe"

Try to print it out. See documentation about UNC on MSDN , and some examples here (or google about UNC Path)

If you have only the default shares, it should be something like

  "\\serverName\C$\WINDOWS\notepad.exe"

OR psexec let you use a different notation, but you have to be careful with double quotes there

  psexec \\serverName"c:\WINDOWS\notepad.exe"

Also, ensure that the "Server" service is running on the target machine.

PsExec starts an executable on a remote system and controls the input and output streams of the executable's process so that you can interact with the executable from the local system. PsExec does so by extracting from its executable image an embedded Windows service named Psexesvc and copying it to the Admin$ share of the remote system. PsExec then uses the Windows Service Control Manager API, which has a remote interface, to start the Psexesvc service on the remote system.

The Admin$ share is created and managed by the "Server" service, so you need it running for psexec to work ( http://windowsitpro.com/systems-management/psexec ).

Just to extend current version of answer. There could be a problem of execution psexec due to OS policy, to enable remote control you need to modify registry:

reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f

You can check this: Remote UAC LocalAccountTokenFilterPolicy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM