Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
58d9846
Invoke-Command with StrictModeVersion parameter, Invoke-Command tests…
Thomas-Yu Dec 3, 2021
521836b
Syntax Fixes
Thomas-Yu Dec 9, 2021
f3d1e55
Unit Test Naming Changes
Thomas-Yu Dec 11, 2021
03952d1
Updated to support piped input
Thomas-Yu Dec 15, 2021
5f54b27
Changes made
Thomas-Yu Dec 21, 2021
d951b40
Updated tests
Thomas-Yu Dec 21, 2021
ea03341
Dotnet files removed
Thomas-Yu Dec 21, 2021
c23e680
Implemented experimental feature
Thomas-Yu Dec 22, 2021
d4020bc
Added experimental attribute for parameter
Thomas-Yu Dec 22, 2021
ca5f673
Syntax
Thomas-Yu Dec 22, 2021
6d919fa
Merge
Thomas-Yu Jan 6, 2022
39b929b
Adjusted test file
Thomas-Yu Dec 23, 2021
d2d3e51
Got rid of unnecessary error check in strictmodeversion parameter
Thomas-Yu Jan 4, 2022
aca32fc
Adjusted InternalCommands.cs file to have ValidateVersion class as in…
Thomas-Yu Jan 5, 2022
0f1fb60
Strictmodeoff added, new test
Thomas-Yu Jan 5, 2022
30693b5
Syntax
Thomas-Yu Jan 5, 2022
1d9bc83
Syntax
Thomas-Yu Jan 5, 2022
543253e
Changes
Thomas-Yu Jan 6, 2022
5173f10
Slight fix
Thomas-Yu Jan 6, 2022
4eff4c9
small fix
Thomas-Yu Jan 6, 2022
94f4a3a
Parameter fixes
Thomas-Yu Jan 6, 2022
0ac2c69
Change didnt get saved for last push
Thomas-Yu Jan 6, 2022
5af61ac
Added psstrictmodeassignment
Thomas-Yu Jan 8, 2022
0af0d9a
-StrictMode off works
Thomas-Yu Jan 11, 2022
36842db
variable name change
Thomas-Yu Jan 11, 2022
d353c9d
Changed field
Thomas-Yu Jan 11, 2022
792a425
.
Thomas-Yu Jan 11, 2022
7d7e665
Revert internalcommands.cs changes
Thomas-Yu Jan 11, 2022
ef9086b
syntax
Thomas-Yu Jan 11, 2022
ca47c60
accepts off
Thomas-Yu Jan 12, 2022
1edfaed
syntax
Thomas-Yu Jan 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ExperimentalFeature
internal const string PSCleanBlockFeatureName = "PSCleanBlock";
internal const string PSAMSIMethodInvocationLogging = "PSAMSIMethodInvocationLogging";
internal const string PSExecFeatureName = "PSExec";
internal const string PSStrictModeAssignment = "PSStrictModeAssignment";

#endregion

Expand Down Expand Up @@ -142,6 +143,9 @@ static ExperimentalFeature()
new ExperimentalFeature(
name: PSExecFeatureName,
description: "Add 'exec' built-in command on Linux and macOS"),
new ExperimentalFeature(
name: PSStrictModeAssignment,
description: "Add support of setting Strict-Mode with Invoke-Command"),
};

EngineExperimentalFeatures = new ReadOnlyCollection<ExperimentalFeature>(engineFeatures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,72 @@ public override SwitchParameter UseSSL
}
}

private sealed class ArgumentToPSVersionTransformationAttribute : ArgumentToVersionTransformationAttribute
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Thomas-Yu , @daxian-dbw For the record, I don't think it was necessary to split the attribute/conversion classes into two separate implementations. Soon,Set-StrictMode will also take the off value for strict mode version, which means merging these two classes back together. In the mean time, there was no harm in having one set of attribute/conversion implementations. Now when Set-StrictMode is updated to also support off this work will have to be refactored yet again.

@Thomas-Yu Please create an Issue in PowerShell repo that adds off to the Set-StrictMode -Version parameter. The committee would like to see it work the same as for Invoke-Command here for consistency, although it is not necessary to include the changes in this PR.

{
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);
}
}
}

/// <summary>
/// Gets or sets strict mode.
/// </summary>
[Experimental(ExperimentalFeature.PSStrictModeAssignment, ExperimentAction.Show)]
[Parameter(ParameterSetName = InvokeCommandCommand.InProcParameterSet)]
[ArgumentToPSVersionTransformation]
[ValidateVersion]
public Version StrictMode
{
get
{
return _strictmodeversion;
}

set
{
_strictmodeversion = value;
}
}

private Version _strictmodeversion = null;

/// <summary>
/// For WSMan session:
/// If this parameter is not specified then the value specified in
Expand Down Expand Up @@ -823,6 +889,8 @@ public virtual SwitchParameter RemoteDebug

#endregion

private Version _savedStrictModeVersion;

#endregion Parameters

#region Overrides
Expand Down Expand Up @@ -946,6 +1014,12 @@ protected override void BeginProcessing()
}
}

if (_strictmodeversion != null)
{
_savedStrictModeVersion = Context.EngineSessionState.CurrentScope.StrictModeVersion;
Context.EngineSessionState.CurrentScope.StrictModeVersion = _strictmodeversion;
}

return;
}

Expand Down Expand Up @@ -1162,7 +1236,19 @@ protected override void ProcessRecord()
}
else if (ParameterSetName.Equals(InvokeCommandCommand.InProcParameterSet) && (_steppablePipeline != null))
{
_steppablePipeline.Process(InputObject);
try
{
_steppablePipeline.Process(InputObject);
}
catch
{
if (_strictmodeversion != null)
{
Context.EngineSessionState.CurrentScope.StrictModeVersion = _savedStrictModeVersion;
}

throw;
}
}
else
{
Expand Down Expand Up @@ -1193,20 +1279,30 @@ protected override void EndProcessing()
{
if (ParameterSetName.Equals(InvokeCommandCommand.InProcParameterSet))
{
if (_steppablePipeline != null)
{
_steppablePipeline.End();
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);
}
}
else
finally
{
ScriptBlock.InvokeUsingCmdlet(
contextCmdlet: this,
useLocalScope: !NoNewScope,
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
dollarUnder: AutomationNull.Value,
input: _input,
scriptThis: AutomationNull.Value,
args: ArgumentList);
if (_strictmodeversion != null)
{
Context.EngineSessionState.CurrentScope.StrictModeVersion = _savedStrictModeVersion;
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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
}
}
}