diff --git a/src/System.Management.Automation/engine/CommonCommandParameters.cs b/src/System.Management.Automation/engine/CommonCommandParameters.cs index 4ae05ac878b..9c283798445 100644 --- a/src/System.Management.Automation/engine/CommonCommandParameters.cs +++ b/src/System.Management.Automation/engine/CommonCommandParameters.cs @@ -78,6 +78,7 @@ public SwitchParameter Debug /// This parameter tells the command what to do when an error occurs. /// [Parameter] + [ValidateNotNull] [Alias("ea")] public ActionPreference ErrorAction { @@ -94,6 +95,7 @@ public ActionPreference ErrorAction /// occurs. /// [Parameter] + [ValidateNotNull] [Alias("wa")] public ActionPreference WarningAction { @@ -115,6 +117,7 @@ public ActionPreference WarningAction /// was added to PowerShell, so "infa" was chosen instead. /// --> [Parameter] + [ValidateNotNull] [Alias("infa")] public ActionPreference InformationAction { diff --git a/test/powershell/Language/Scripting/ActionPreference.Tests.ps1 b/test/powershell/Language/Scripting/ActionPreference.Tests.ps1 index bdad4344b7b..ab6fc2c56a5 100644 --- a/test/powershell/Language/Scripting/ActionPreference.Tests.ps1 +++ b/test/powershell/Language/Scripting/ActionPreference.Tests.ps1 @@ -186,6 +186,13 @@ Describe "Tests for (error, warning, etc) action preference" -Tags "CI" { { New-Item @params } | Should -Throw -ErrorId "NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand" Remove-Item "$testdrive\test.txt" -Force } + + It "Parameter binding throws correctly (no NRE) if argument is null" { + $NullVariable = $null + { Test-Path .\noexistfile.ps1 -ErrorAction $NullVariable } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestPathCommand" + { Test-Path .\noexistfile.ps1 -InformationAction $NullVariable } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestPathCommand" + { Test-Path .\noexistfile.ps1 -WarningAction $NullVariable } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestPathCommand" + } } Describe 'ActionPreference.Break tests' -tag 'CI' {