Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Write-Warning causes errors in functions called with -WarningAction Ignore  #4120

@Jaykul

Description

@Jaykul

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions