Fix Start-Process -Credential -Wait to work on Windows (#19082)

* address Ilya's feedback, simplify to just WaitForExit() when cred is used

* Update src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs

Co-authored-by: Ilya <darpa@yandex.ru>

---------

Co-authored-by: Ilya <darpa@yandex.ru>
This commit is contained in:
Steve Lee
2023-02-02 16:15:53 -08:00
committed by GitHub
co-authored by Ilya
parent 9d8cc0c62d
commit 7fe5cb3e35
@@ -2039,22 +2039,30 @@ namespace Microsoft.PowerShell.Commands
#if UNIX
process.WaitForExit();
#else
_waithandle = new ManualResetEvent(false);
// Create and start the job object
ProcessCollection jobObject = new();
if (jobObject.AssignProcessToJobObject(process))
if (_credential is not null)
{
// 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;
// If we are running as a different user, we cannot use a job object, so just wait on the process
process.WaitForExit();
}
else
{
_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
}
}