PsExec hang while being executed from a very simple c# or c++ gui program compiled as “windows application”

前端 未结 6 731
日久生厌
日久生厌 2021-01-18 12:04

I am experiencing PsExec hang while being executed from a very simple c# or c++ gui program compiled as \"windows application\" (not as \"console application\"). Under sect

6条回答
  •  心在旅途
    2021-01-18 12:12

    Synchronous read on the stream works:

            ProcessStartInfo startInfo = new ProcessStartInfo("psexec.exe", @"\\localhost cmd.exe /c dir c:\windows\*.*");
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardOutput = true;
            //startInfo.RedirectStandardError = true;
            //startInfo.RedirectStandardInput = true;
    
            Process proc = new Process();
            proc.StartInfo = startInfo;
            //proc.ErrorDataReceived += new DataReceivedEventHandler(DataReceiveHandler);
            //proc.OutputDataReceived += new DataReceivedEventHandler(DataReceiveHandler);
            proc.Start();
            //proc.BeginErrorReadLine();
            //proc.BeginOutputReadLine();
            string output = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit();
            Console.WriteLine(output);
            Console.WriteLine("Exit code = {0}", proc.ExitCode);
    

    Even here, note that the ReadToEnd() should be done before the WaitForExit().

    I believe PSExec always had problems like this. When running under Java service, we used to redirect the output to nul and couldn't get the output of the running process, but can get the output of PSExec itself.

    Refer to below given discussions:

    http://forum.sysinternals.com/psexec-always-hangs-when-run-from-java_topic5013.html

    http://forum.sysinternals.com/unusual-problem-with-psexecexe_topic6655.html

    Edit:

    Note on PSEXESVC cleanup: Delete the PSEXESVC.EXE file at C:\Windows ( or C:\Windows\system32 or both ) after killing the hung PSEXESVC process. Lingering process / file cause more problems.

提交回复
热议问题