diff --git a/experimental-feature-linux.json b/experimental-feature-linux.json index 94d88a4a8d..4dee949650 100644 --- a/experimental-feature-linux.json +++ b/experimental-feature-linux.json @@ -2,6 +2,5 @@ "PSCommandNotFoundSuggestion", "PSLoadAssemblyFromNativeCode", "PSNativeCommandErrorActionPreference", - "PSStrictModeAssignment", "PSSubsystemPluginModel" ] diff --git a/experimental-feature-windows.json b/experimental-feature-windows.json index 94d88a4a8d..4dee949650 100644 --- a/experimental-feature-windows.json +++ b/experimental-feature-windows.json @@ -2,6 +2,5 @@ "PSCommandNotFoundSuggestion", "PSLoadAssemblyFromNativeCode", "PSNativeCommandErrorActionPreference", - "PSStrictModeAssignment", "PSSubsystemPluginModel" ] diff --git a/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs b/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs index 6caea95de5..8eaf382207 100644 --- a/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs +++ b/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs @@ -22,7 +22,6 @@ namespace System.Management.Automation internal const string EngineSource = "PSEngine"; internal const string PSNativeCommandErrorActionPreferenceFeatureName = "PSNativeCommandErrorActionPreference"; - internal const string PSStrictModeAssignment = "PSStrictModeAssignment"; #endregion @@ -117,9 +116,6 @@ namespace System.Management.Automation new ExperimentalFeature( name: PSNativeCommandErrorActionPreferenceFeatureName, description: "Native commands with non-zero exit codes issue errors according to $ErrorActionPreference when $PSNativeCommandUseErrorActionPreference is $true"), - new ExperimentalFeature( - name: PSStrictModeAssignment, - description: "Add support of setting Strict-Mode with Invoke-Command"), }; EngineExperimentalFeatures = new ReadOnlyCollection(engineFeatures); diff --git a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs index 804355d2e8..a2a2397cb7 100644 --- a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs @@ -263,72 +263,6 @@ namespace Microsoft.PowerShell.Commands } } - private sealed class ArgumentToPSVersionTransformationAttribute : ArgumentToVersionTransformationAttribute - { - protected override bool TryConvertFromString(string versionString, [NotNullWhen(true)] out Version version) - { - if (string.Equals("off", versionString, StringComparison.OrdinalIgnoreCase)) - { - version = new Version(0, 0); - return true; - } - - if (string.Equals("latest", versionString, StringComparison.OrdinalIgnoreCase)) - { - version = PSVersionInfo.PSVersion; - return true; - } - - return base.TryConvertFromString(versionString, out version); - } - } - - private static readonly Version s_OffVersion = new Version(0, 0); - - private sealed class ValidateVersionAttribute : ValidateArgumentsAttribute - { - protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics) - { - Version version = arguments as Version; - if (version == s_OffVersion) - { - return; - } - - if (version == null || !PSVersionInfo.IsValidPSVersion(version)) - { - // No conversion succeeded so throw an exception... - throw new ValidationMetadataException( - "InvalidPSVersion", - null, - Metadata.ValidateVersionFailure, - arguments); - } - } - } - - /// - /// Gets or sets strict mode. - /// - [Experimental(ExperimentalFeature.PSStrictModeAssignment, ExperimentAction.Show)] - [Parameter(ParameterSetName = InvokeCommandCommand.InProcParameterSet)] - [ArgumentToPSVersionTransformation] - [ValidateVersion] - public Version StrictMode - { - get - { - return _strictmodeversion; - } - - set - { - _strictmodeversion = value; - } - } - - private Version _strictmodeversion = null; - /// /// For WSMan session: /// If this parameter is not specified then the value specified in @@ -908,8 +842,6 @@ namespace Microsoft.PowerShell.Commands #endregion - private Version _savedStrictModeVersion; - #endregion Parameters #region Overrides @@ -1033,12 +965,6 @@ namespace Microsoft.PowerShell.Commands } } - if (_strictmodeversion != null) - { - _savedStrictModeVersion = Context.EngineSessionState.CurrentScope.StrictModeVersion; - Context.EngineSessionState.CurrentScope.StrictModeVersion = _strictmodeversion; - } - return; } @@ -1255,19 +1181,7 @@ namespace Microsoft.PowerShell.Commands } else if (ParameterSetName.Equals(InvokeCommandCommand.InProcParameterSet) && (_steppablePipeline != null)) { - try - { - _steppablePipeline.Process(InputObject); - } - catch - { - if (_strictmodeversion != null) - { - Context.EngineSessionState.CurrentScope.StrictModeVersion = _savedStrictModeVersion; - } - - throw; - } + _steppablePipeline.Process(InputObject); } else { @@ -1298,30 +1212,20 @@ namespace Microsoft.PowerShell.Commands { if (ParameterSetName.Equals(InvokeCommandCommand.InProcParameterSet)) { - try - { - if (_steppablePipeline != null) - { - _steppablePipeline.End(); - } - else - { - ScriptBlock.InvokeUsingCmdlet( - contextCmdlet: this, - useLocalScope: !NoNewScope, - errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe, - dollarUnder: AutomationNull.Value, - input: _input, - scriptThis: AutomationNull.Value, - args: ArgumentList); - } - } - finally + if (_steppablePipeline != null) { - if (_strictmodeversion != null) - { - Context.EngineSessionState.CurrentScope.StrictModeVersion = _savedStrictModeVersion; - } + _steppablePipeline.End(); + } + else + { + ScriptBlock.InvokeUsingCmdlet( + contextCmdlet: this, + useLocalScope: !NoNewScope, + errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe, + dollarUnder: AutomationNull.Value, + input: _input, + scriptThis: AutomationNull.Value, + args: ArgumentList); } } else diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Invoke-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Invoke-Command.Tests.ps1 deleted file mode 100644 index 1765e5d821..0000000000 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Invoke-Command.Tests.ps1 +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. - -Describe "Invoke-Command" -Tags "CI" { - Context "StrictMode tests" { - BeforeAll { - $skipTest = !($EnabledExperimentalFeatures -contains "PSStrictModeAssignment"); - If (Test-Path Variable:InvokeCommand__Test) { - Remove-Item Variable:InvokeCommand__Test - } - } - - It "Setting -StrictMode parameter with uninitialized variable throws error" -skip:$skipTest { - { Invoke-Command -StrictMode 3.0 {$InvokeCommand__Test} } | Should -Throw -ErrorId 'VariableIsUndefined' - } - - It "Setting -StrictMode parameter with initialized variable does not throw error" -skip:$skipTest { - $InvokeCommand__Test = 'Something' - Invoke-Command -StrictMode 3.0 {$InvokeCommand__Test} | Should -Be 'Something' - Remove-Item Variable:InvokeCommand__Test - } - - It "-StrictMode parameter sets StrictMode back to original state after process completes" -skip:$skipTest { - { Invoke-Command -StrictMode 3.0 {$InvokeCommand__Test} } | Should -Throw -ErrorId 'VariableIsUndefined' - { Invoke-Command {$InvokeCommand__Test} } | Should -Not -Throw - } - - It "-StrictMode parameter works on piped input" -skip:$skipTest { - "There" | Invoke-Command -ScriptBlock { "Hello $input" } -StrictMode 3.0 | Should -Be 'Hello There' - { "There" | Invoke-Command -ScriptBlock { "Hello $InvokeCommand__Test" } -StrictMode 3.0 } | Should -Throw -ErrorId 'VariableIsUndefined' - } - - It "-StrictMode latest works" -skip:$skipTest { - { Invoke-Command -StrictMode latest {$InvokeCommand__Test} } | Should -Throw -ErrorId 'VariableIsUndefined' - } - - It "-StrictMode off works" -skip:$skipTest { - { Invoke-Command -StrictMode off {$InvokeCommand__Test} } | Should -Not -Throw - } - } -}