-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Description
In both PS5.1 and PS6b3 the Ignore value is allowed on the common parameter -WarningAction Ignore, but it causes a (non-terminating) error if the script calls Write-Warning -- but not if the script uses $PSCmdlet.WriteWarning instead...
Example function:
function Test-Warning {
[CmdletBinding()]param()
Write-Host $WarningPreference -ForegroundColor Cyan
$PSCmdlet.WriteWarning("PSCmdlet.WriteWarning")
Write-Warning "Write-Warning"
Write-Host "-------`n"
}When you invoke it (in this example, I redefined the function all on one line so you could clearly see from the error message that only Write-Warning is affected):
<# PS:#> Test-Warning -WarningAction Ignore
HOST: Ignore
ERROR: Write-Warning : The value Ignore is not supported for an ActionPreference variable.
The provided value should be used only as a value for a preference parameter, and has been replaced by
the default value. For more information, see the Help topic, "about_Preference_Variables."
At line:2 char:50
+ ... rning("PSCmdlet.WriteWarning"); Write-Warning "Write-Warning"; Write- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Warning], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.WriteWarningCommand
-------
You can see that the error text is incorrectly assuming that the user has set the ActionPrefence variable, and states that the value will be treated as the default instead, but the warning is not output.
You can also verify that this only affects Write-Warning and not the use of $PSCmdlet.WriteWarning
Expected behavior
Write-Warning should work the same as $PSCmdlet.WriteWarning does ... allowing authors of "advanced" cmdletbinding scripts and functions to use Write-Warning instead of having to learn the method syntax.