From 7e46944034f6a3f255c89c1eb5278934f301a7af Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Sat, 30 Mar 2024 13:06:03 +0000 Subject: [PATCH 1/2] Start-Process: Fix -Credential error as non-admin When using Start-Process -Credential in a non-administrator context with a different user's credentials the code currently throws an exception when attempting to get the new process' handle. This change treats this error as a warning only if -PassThru was specified as it only affects certain members on the returned object like ExitCode but others like WaitForExit() still work fine. --- .../commands/management/Process.cs | 16 +++++++++++++++- .../resources/ProcessResources.resx | 3 +++ 2 files changed, 18 insertions(+), 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 197e3662c25..a774d09ae7c 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -2109,7 +2109,21 @@ protected override void BeginProcessing() // "Process was not started by this object, so requested information cannot be determined." // Fetching the process handle will trigger the `Process` object to update its internal state by calling `SetProcessHandle`, // the result is discarded as it's not used later in this code. - _ = process.Handle; + try + { + _ = process.Handle; + } + catch (Win32Exception e) + { + // If the caller was not an admin and the process was started with another user's credentials .NET + // won't be able to retrieve the process handle. As this is not a critical failure we treat this as + // a warning. + if (PassThru) + { + string msg = StringUtil.Format(ProcessResources.FailedToCreateProcessObject, e.Message); + WriteWarning(msg); + } + } // Resume the process now that is has been set up. processInfo.Resume(); diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/ProcessResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/ProcessResources.resx index a6f6d286dbe..b4c1ddbccf8 100644 --- a/src/Microsoft.PowerShell.Commands.Management/resources/ProcessResources.resx +++ b/src/Microsoft.PowerShell.Commands.Management/resources/ProcessResources.resx @@ -189,6 +189,9 @@ This command cannot be run completely because the system cannot find all the information required. + + Failed to retrieve the new process handle: "{0}". The Process object outputted may have some properties and methods that do not work properly. + This command cannot be run due to error 1783. The possible cause of this error can be using of a non-existing user "{0}". Please give a valid user and run your command again. From c12a42f18c80d050c01a4330b806775c996e67d0 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Fri, 5 Apr 2024 06:44:23 +1000 Subject: [PATCH 2/2] Change warning to debug --- .../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 a774d09ae7c..73f00b5acc6 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -2121,7 +2121,7 @@ protected override void BeginProcessing() if (PassThru) { string msg = StringUtil.Format(ProcessResources.FailedToCreateProcessObject, e.Message); - WriteWarning(msg); + WriteDebug(msg); } }