PowerShell v2 CTP3 advanced function parameter '-WarningAction' not working


i'm currently in process of writing powershell scripts managing our environment, , want make use of 'warningaction' common parameter. doesn't seem work :-(.

i'm using [cmdletbinding()] attribute in script enable common parameters (http://msdn.microsoft.com/en-us/library/dd901844(vs.85).aspx).

consider following powershell v2 ctp3 script test.ps1:

==============

[cmdletbinding()]
param()

write-host "warning: $warningpreference"
write-host "error: $erroractionpreference"

write-warning "b"
write-error "c"
==============

if execute script follows:

==============
ps > .\test.ps1 -erroraction silentlycontinue
warning: continue
error: silentlycontinue
warning: b
==============

as expected, $erroractionpreference set 'silentlycontinue', , output write-error cmd suppressed.

however, if 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, , output write-warning cmd not suppressed expected.

does happen else? or there obvious i'm missing?

running get-help indicates -warningaction supported:

==============
ps d:\> get-help .\test.ps1
test.ps1 [-verbose] [-debug] [-erroraction <actionpreference>] [-warningaction <actionpreference>] [-errorvariable <string>] [-warningvariable <string>] [-outvariable <string>] [-outbuffer <int32>]
==============

thanks
michael

note: have posted version of question in scripting guys forum (see link below). suggested post here well.
http://social.technet.microsoft.com/forums/en/itcg/thread/1e7ea871-432f-4650-ac1e-5a5a5e663f40

believe or not, parameters take effect need use special methods documented in about_functions_advanced_methods methods of special magic variable $pscmdlet ...

[cmdletbinding()] param()  write-host "warning: $warningpreference" write-host "error: $erroractionpreference"  $pscmdlet.writewarning( "b" ) $pscmdlet.writeerror( "c" )


documentation says "can use various write cmdlets," @ least in case, it's wrong. :-(


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Event ID 64,77,1008 Certificates Events Windows Server 2008, 2008R2