From 3e8f71abb60349b53bb6481ad5255901a9e27fbb Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 2 Feb 2023 06:44:59 -0800 Subject: [PATCH 1/2] address Ilya's feedback, simplify to just WaitForExit() when cred is used --- .../commands/management/Process.cs | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index 2bdb5fcc2b6..e9588629f0a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -2039,21 +2039,29 @@ protected override void BeginProcessing() #if UNIX process.WaitForExit(); #else - _waithandle = new ManualResetEvent(false); - - // Create and start the job object - ProcessCollection jobObject = new(); - if (jobObject.AssignProcessToJobObject(process)) + if (_credential != null) { - // Wait for the job object to finish - jobObject.WaitOne(_waithandle); + // If we are running as a different user, we cannot use a job object, so just wait on the process + process.WaitForExit(); } - else if (!process.HasExited) + else { - // WinBlue: 27537 Start-Process -Wait doesn't work in a remote session on Windows 7 or lower. - process.Exited += myProcess_Exited; - process.EnableRaisingEvents = true; - process.WaitForExit(); + _waithandle = new ManualResetEvent(false); + + // Create and start the job object + ProcessCollection jobObject = new(); + if (jobObject.AssignProcessToJobObject(process)) + { + // Wait for the job object to finish + jobObject.WaitOne(_waithandle); + } + else if (!process.HasExited) + { + // WinBlue: 27537 Start-Process -Wait doesn't work in a remote session on Windows 7 or lower. + process.Exited += myProcess_Exited; + process.EnableRaisingEvents = true; + process.WaitForExit(); + } } #endif } From 585411822edf56b8f8f35bb5e00f8abfdda0e6e0 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 2 Feb 2023 08:37:14 -0800 Subject: [PATCH 2/2] Update src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs Co-authored-by: Ilya --- .../commands/management/Process.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index e9588629f0a..70596e263da 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -2039,7 +2039,7 @@ protected override void BeginProcessing() #if UNIX process.WaitForExit(); #else - if (_credential != null) + if (_credential is not null) { // If we are running as a different user, we cannot use a job object, so just wait on the process process.WaitForExit();