mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
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:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user