PowerShell v2 advanced function parameter '-WarningAction' not working
I'm currently in the process of writing some PowerShell scripts for managing our environment, and want to make use of the 'WarningAction' common parameter. However it doesn't seem to work :-(.
Consider the following PowerShell v2 script test.ps1:
==============[CmdletBinding()]
==============
param()
Write-Host "warning: $WarningPreference"
Write-Host "error: $ErrorActionPreference"
Write-Warning "b"
Write-Error "c"
If I execute the script as follows:
==============
PS > .\test.ps1 -ErrorAction SilentlyContinue
==============
warning: Continue
error: SilentlyContinue
WARNING: b
As expected, the $ErrorActionPreference is set to 'SilentlyContinue', and the output from the Write-Error cmd is suppressed.
However, if I execute:
==============
PS > .\test.ps1 -WarningAction SilentlyContinue
==============
warning: Continue
error: Continue
WARNING: b
D:\Misc\scripts\PowerShell\test.ps1 : c
At line:1 char:11
+ .\test.ps1 <<<< -WarningAction SilentlyContinue
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
The $WarningPreference isn't set, and the output from the Write-Warning cmd is not suppressed as would be expected.
Does this happen for anyone else? Or is there something obvious I'm missing?
Thanks
Michael- Edited byMichael Ward27 Thursday, October 29, 2009 10:03 PMFixed up formatting
- Edited byMichael Ward27 Thursday, October 29, 2009 10:13 PMAdded == between code blocks
- Edited byMichael Ward27 Thursday, October 29, 2009 10:10 PMUpdate formatting, 2nd attempt - wish there was a preview button!
Answers
- Just to follow up - I posted this in the PowerShell forum and Joel promptly answered. See the following post:
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/19d27481-1acc-412c-9085-9f89d00fa4e9
To quote Joel:
Believe it or not, for that parameters to take effect you need to use the special methods documented in about_Functions_Advanced_Methods which are all methods of the special magic variable $PSCmdlet ...
[CmdletBinding()] param() Write-Host "warning: $WarningPreference" Write-Host "error: $ErrorActionPreference" $PSCmdlet.WriteWarning( "b" ) $PSCmdlet.WriteError( "c" )
The documentation actually says that you "can also use the various Write cmdlets," but at least in this case, it's wrong. :-(- Marked As Answer byMichael Ward27 Wednesday, November 04, 2009 1:48 AM
All Replies
- WarningAction is not the same as $WarningPreference.
WarningAction is one of the "Common Paramaters" that can be passed to a cmdlet that accepts that paramater - your script doesn't accept the paramater -WarningAction
http://msdn.microsoft.com/en-us/library/system.management.automation.internal.commonparameters.warningaction(VS.85).aspx
Try this:
[CmdletBinding()] param($in) $WarningPreference = $in Write-Host "warning: $WarningPreference" Write-Warning "b"
Now:
.\test.ps1 "SilentlyContinue"
Then:
.\test.ps1 "Continue"
Karl - The -WarningAction parameter is supposed to set the $WarningPreference variable. See Example 4 here: http://technet.microsoft.com/en-us/library/dd347569.aspx
By specifying [CmdletBinding()], -ErrorAction is setting the $ErrorActionPreference OK, however -WarningAction is not setting $WarningPreference.
I'm not sure if I should be specifying something else with [CmdletBinding()], however if I use Get-Help on test.ps1, it tells me WarningAction is supported:
PS D:\> Get-Help .\test.ps1
test.ps1 [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>]
You're right - I could set the $WarningPreference by using a custom parameter, however I'd prefer to get it going using the standard -WarningAction parameter.
Michael- Edited byMichael Ward27 Sunday, November 01, 2009 7:13 PMadded get-help output
Michael;
You are missing the point :)
Write-Warning takes the "Common Parameter" -WarningAction
Nowhere in your link does it suggest that -WarningAction sets the $WarningPreference
Trust me., you need to either modify your script to accept a -WarningAction parameter or set $WarningPreference :)
KarlSorry Karl - I think you are!
In example 4, it specifically states:
"The WarningAction common parameter overrides the value of the $WarningPreference only for that particular command"
Using [CmdletBinding()] is supposed enable all the common parameters for my script, including -WarningAction.
As I've already said, -ErrorAction is setting $ErrorActionPreference OK, but -WarningAction is not setting $WarningPreference as the documentation specifies it should.
Modifying my script to accept a pseudo -WarningAction parameter isn't really an option as I also want to be able to use -WarningVariable to return the warning to the calling script. I'm assuming if I can get -WarningAction working, -WarningVariable will follow (-ErrorVariable currently does work).
Michael
- Michael;
Could you show me where you fine that "Using [CmdletBinding()] is supposed enable all the common parameters"?
Addidionally, you might post this in the PowerShell forum:
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/threads - See here: http://msdn.microsoft.com/en-us/library/dd901844(VS.85).aspx
"These parameters are also added to provider cmdlets and to functions that are decorated with the CmdletBinding attribute."
I haven't decided yet whether this is a bug or I'm just missing something. Thanks for pointing out the other forum (didn't know it existed), will ask this there too.
Michael - Just to follow up - I posted this in the PowerShell forum and Joel promptly answered. See the following post:
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/19d27481-1acc-412c-9085-9f89d00fa4e9
To quote Joel:
Believe it or not, for that parameters to take effect you need to use the special methods documented in about_Functions_Advanced_Methods which are all methods of the special magic variable $PSCmdlet ...
[CmdletBinding()] param() Write-Host "warning: $WarningPreference" Write-Host "error: $ErrorActionPreference" $PSCmdlet.WriteWarning( "b" ) $PSCmdlet.WriteError( "c" )
The documentation actually says that you "can also use the various Write cmdlets," but at least in this case, it's wrong. :-(- Marked As Answer byMichael Ward27 Wednesday, November 04, 2009 1:48 AM

