Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd6b4d8

Browse files
pwshBotdaxian-dbwTravisEz13
authored
[release/v7.5] Set standard handles explicitly when starting a process with-NoNewWindow (#25324)
Co-authored-by: Dongbo Wang <dongbow@microsoft.com>Co-authored-by: Travis Plunk <travis.plunk@microsoft.com>
1 parent8249040 commitcd6b4d8

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

‎src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,7 @@ public SwitchParameter LoadUserProfile
16771677
privateSwitchParameter_loaduserprofile=SwitchParameter.Present;
16781678

16791679
/// <summary>
1680-
/// Starts process ina new window.
1680+
/// Starts process inthe current console window.
16811681
/// </summary>
16821682
[Parameter(ParameterSetName="Default")]
16831683
[Alias("nnw")]
@@ -1965,7 +1965,9 @@ protected override void BeginProcessing()
19651965

19661966
startInfo.WindowStyle=_windowstyle;
19671967

1968-
if(_nonewwindow)
1968+
// When starting a process as another user, the 'CreateNoWindow' property value is ignored and a new window is created.
1969+
// See details at https://learn.microsoft.com/dotnet/api/system.diagnostics.processstartinfo.createnowindow?view=net-9.0#remarks
1970+
if(_nonewwindow&&_credentialisnull)
19691971
{
19701972
startInfo.CreateNoWindow=_nonewwindow;
19711973
}
@@ -2413,33 +2415,60 @@ private static byte[] ConvertEnvVarsToByteArray(StringDictionary sd)
24132415

24142416
privatevoidSetStartupInfo(ProcessStartInfostartinfo,refProcessNativeMethods.STARTUPINFOlpStartupInfo,refintcreationFlags)
24152417
{
2416-
boolhasRedirection=false;
2418+
// If we are starting a process using the current console window, we need to set its standard handles
2419+
// explicitly when they are not redirected because otherwise they won't be set and the new process will
2420+
// fail with the "invalid handle" error.
2421+
//
2422+
// However, if we are starting a process with a new console window, we should not explicitly set those
2423+
// standard handles when they are not redirected, but instead let Windows figure out the default to use
2424+
// when creating the process. Otherwise, the standard input handles of the current window and the new
2425+
// window will get weirdly tied together and cause problems.
2426+
boolhasRedirection=startinfo.CreateNoWindow
2427+
||_redirectstandardinputis notnull
2428+
||_redirectstandardoutputis notnull
2429+
||_redirectstandarderroris notnull;
2430+
24172431
// RedirectionStandardInput
24182432
if(_redirectstandardinput!=null)
24192433
{
2420-
hasRedirection=true;
24212434
startinfo.RedirectStandardInput=true;
24222435
_redirectstandardinput=ResolveFilePath(_redirectstandardinput);
24232436
lpStartupInfo.hStdInput=GetSafeFileHandleForRedirection(_redirectstandardinput,FileMode.Open);
24242437
}
2438+
elseif(startinfo.CreateNoWindow)
2439+
{
2440+
lpStartupInfo.hStdInput=newSafeFileHandle(
2441+
ProcessNativeMethods.GetStdHandle(-10),
2442+
ownsHandle:false);
2443+
}
24252444

24262445
// RedirectionStandardOutput
24272446
if(_redirectstandardoutput!=null)
24282447
{
2429-
hasRedirection=true;
24302448
startinfo.RedirectStandardOutput=true;
24312449
_redirectstandardoutput=ResolveFilePath(_redirectstandardoutput);
24322450
lpStartupInfo.hStdOutput=GetSafeFileHandleForRedirection(_redirectstandardoutput,FileMode.Create);
24332451
}
2452+
elseif(startinfo.CreateNoWindow)
2453+
{
2454+
lpStartupInfo.hStdOutput=newSafeFileHandle(
2455+
ProcessNativeMethods.GetStdHandle(-11),
2456+
ownsHandle:false);
2457+
}
24342458

24352459
// RedirectionStandardError
24362460
if(_redirectstandarderror!=null)
24372461
{
2438-
hasRedirection=true;
24392462
startinfo.RedirectStandardError=true;
24402463
_redirectstandarderror=ResolveFilePath(_redirectstandarderror);
24412464
lpStartupInfo.hStdError=GetSafeFileHandleForRedirection(_redirectstandarderror,FileMode.Create);
24422465
}
2466+
elseif(startinfo.CreateNoWindow)
2467+
{
2468+
lpStartupInfo.hStdError=newSafeFileHandle(
2469+
ProcessNativeMethods.GetStdHandle(-12),
2470+
ownsHandle:false);
2471+
}
24432472

24442473
if(hasRedirection)
24452474
{
@@ -2881,6 +2910,9 @@ internal struct JOBOBJECT_BASIC_PROCESS_ID_LIST
28812910

28822911
internalstaticclassProcessNativeMethods
28832912
{
2913+
[DllImport(PinvokeDllNames.GetStdHandleDllName,SetLastError=true)]
2914+
publicstaticexternIntPtrGetStdHandle(intwhichHandle);
2915+
28842916
[DllImport(PinvokeDllNames.CreateProcessWithLogonWDllName,CharSet=CharSet.Unicode,SetLastError=true,ExactSpelling=true)]
28852917
[return:MarshalAs(UnmanagedType.Bool)]
28862918
internalstaticexternboolCreateProcessWithLogonW(stringuserName,

‎test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,17 @@ Describe "Environment Tests" -Tags "Feature" {
241241
}
242242
}
243243
}
244+
245+
Describe"Bug fixes"-Tags"CI" {
246+
247+
## https://github.com/PowerShell/PowerShell/issues/24986
248+
It"Error redirection along with '-NoNewWindow' should work for Start-Process"-Skip:(!$IsWindows) {
249+
$errorFile=Join-Path-Path$TestDrive-ChildPath error.txt
250+
$out= pwsh-noprofile-c"Start-Process -Wait -NoNewWindow -RedirectStandardError$errorFile -FilePath cmd -ArgumentList '/C echo Hello'"
251+
252+
## 'Hello' should be sent to standard output; 'error.txt' file should be created but empty.
253+
$out| Should-BeExactly"Hello"
254+
Test-Path-Path$errorFile| Should-BeTrue
255+
(Get-Item$errorFile).Length| Should-Be0
256+
}
257+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp