From dd1fe759d42798b2f9baa82f2eb672d12d6c7d16 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Thu, 19 Oct 2023 14:35:40 -0700 Subject: [PATCH] Fix implicit remoting proxy cmdlets to act on common parameters (#20367) (#20530) --- .../commands/utility/ImplicitRemotingCommands.cs | 14 ++++++++++++-- .../CompatiblePSEditions.Module.Tests.ps1 | 11 +++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs index 1631bb1876..f92aa919c4 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs @@ -1300,13 +1300,23 @@ namespace Microsoft.PowerShell.Commands parameterType); } - private static bool IsProxyForCmdlet(Dictionary parameters) + private bool IsProxyForCmdlet(Dictionary parameters) { // we are not sending CmdletBinding/DefaultParameterSet over the wire anymore // we need to infer IsProxyForCmdlet from presence of all common parameters - foreach (string commonParameterName in Cmdlet.CommonParameters) + // need to exclude `ProgressAction` which may not exist for downlevel platforms + bool isDownLevelRemote = Session.Runspace is RemoteRunspace remoteRunspace + && remoteRunspace.ServerVersion is not null + && remoteRunspace.ServerVersion <= new Version(7, 3); + + foreach (string commonParameterName in CommonParameters) { + if (isDownLevelRemote && commonParameterName == "ProgressAction") + { + continue; + } + if (!parameters.ContainsKey(commonParameterName)) { return false; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/CompatiblePSEditions.Module.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/CompatiblePSEditions.Module.Tests.ps1 index a9c9b69b20..d4423b034b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/CompatiblePSEditions.Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/CompatiblePSEditions.Module.Tests.ps1 @@ -1532,4 +1532,15 @@ Describe "WinCompat importing should check availablity of built-in modules" -Tag $result[4] | Should -BeExactly 'ConvertFrom-String' $result[5] | Should -BeExactly 'CFS' } + + It 'ErrorAction should be used for cmdlet' { + try { + $out = Invoke-Expression 'get-AppLockerFileInformation NoSuch.exe -ErrorAction Stop; "after"' + } + catch { + # do nothing as we expect an error, but execution should not continue + } + + $out | Should -Not -Contain 'after' + } }