Test for $Verbose switch on Advanced function.
0
PEOPLE
I found that if I use:
[CmdletBinding()]
param
(
[string]$AParm,
[switch]$Verbose,
...
Then I get an error indicating that Verbose is already defined. This is a common parameter, so that makes sense to me. However, if I pass the script "-verbose" and then I test for $Verbose in the subsequent script, it is not $true. How can I check for a set switch on a common parameter?
I know I could drop the CmdletBinding attribute, because then it works, but if I use a mandatory parameter, it seems to force CmdletBinding anyway.
Thanks!
Replies
It's actually easier than you think - you don't have to check for $verbose at all. Just use Write-Verbose; if your cmdlet is run with -verbose, it'll output; if not, it won't.
function test {
[CmdletBinding()]
param (
[string]$parm
)
PROCESS {
write $parm
write-verbose $parm
}
}
test -parm test
test -parm test -verbose
I tried that, and it works, but I am using my own logging function so would prefer not to use write-verbose. So as far as you know, you can't query the common paramter switch?
Thanks, I apprecate the help Don.
Not to my knowledge. From within a script cmdlet, you're not meant to override the common parameters. You'd be better coming up with a unique name like -extradetail or -log or something. From a usability perspective, it's a really bad idea to take a common param and give it a different behavior - the -verbose switch should always do the same thing on every cmdlet; that's why it's a "common parameter." Even a normal .NET cmdlet doesn't get to define that - it has to take the built-in functionality.
OK, thanks!







