7

Is there an easy way to make the -Verbose switch "passthrough" to other function calls in Powershell?

I know I can probably search $PSBoundParameters for the flag and do an if statement:

[CmdletBinding()]
Function Invoke-CustomCommandA {
    Write-Verbose "Invoking Custom Command A..."

    if ($PSBoundParameters.ContainsKey("Verbose")) {
        Invoke-CustomCommandB -Verbose
    } else {
        Invoke-CustomCommandB
    }
}

Invoke-CustomCommandA -Verbose

It seems rather messy and redundant to do it this way however... Thoughts?

3 Answers 3

9

One way is to use $PSDefaultParameters at the top of your advanced function:

$PSDefaultParameterValues = @{"*:Verbose"=($VerbosePreference -eq 'Continue')}

Then every command you invoke with a -Verbose parameter will have it set depending on whether or not you used -Verbose when you invoked your advanced function.

If you have just a few commands the do this:

$verbose = [bool]$PSBoundParameters["Verbose"]
Invoke-CustomCommandB -Verbose:$verbose
7
  • I would do a $PSDefaultParameterValues = $PSDefaultParameterValues.clone() first, otherwise you're probably going to end up setting it for the global scope and not just the function and it's child scopes.
    – mjolinor
    Dec 5, 2013 at 11:21
  • Why? $PSDefaultParamaterValues obeys the same scoping rules that other variables obey. If I had done $global:PSDefaultParameterValues=... then it would write to the value to the globally scoped $PSDefaultParameterValues.
    – Keith Hill
    Dec 5, 2013 at 17:10
  • You're adding a key to a hash table, not creating a new variable. If that hash table doesn't exist in the function scope it will start looking for it in a parent scope, not create a new one.
    – mjolinor
    Dec 5, 2013 at 17:46
  • I beg to differ. Assigning to a variable within a scope which has the same name as a global variable will do a "copy-on-write" op - essentially creating a new variable that shadows the global variable. Try the experiment. I have. :-)
    – Keith Hill
    Dec 5, 2013 at 19:33
  • You're right. It does create a new hash table. But you also lose any default parameter settings already set. If you want to retain those in the function you need to clone the existing $PSDefaultParameterValues in the function, and then add the new entry to that.
    – mjolinor
    Dec 5, 2013 at 19:45
2

I began using KeithHill's $PSDefaultParameterValues technique in some powershell modules. I ran into some pretty surprising behavior which I'm pretty sure resulted from the effect of scope and $PSDefaultParameterValues being a sort-of global variable. I ended up writing a cmdlet called Get-CommonParameters (alias gcp) and using splat parameters to achieve explicit and terse cascading of -Verbose (and the other common parameters). Here is an example of how that looks:

function f1 {
    [CmdletBinding()]
    param()
    process
    {
        $cp = &(gcp)
        f2 @cp
        # ... some other code ...
        f2 @cp
    }
}
function f2 {
    [CmdletBinding()]
    param()
    process
    {
        Write-Verbose 'This gets output to the Verbose stream.'
    }
}
f1 -Verbose

The source for cmdlet Get-CommonParameters (alias gcp) is in this github repository.

1

How about:

$vb = $PSBoundParameters.ContainsKey('Verbose')
Invoke-CustomCommandB -Verbose:$vb
3
  • 1
    It turns out that doesn't work correctly if the user calls the advanced function with -Verbose:$false. Admittedly, that's a bit of a corner case. :-)
    – Keith Hill
    Dec 5, 2013 at 23:13
  • I have been using the following succesfully even with -Verbose:$false : If($PSCmdlet.MyInvocation.BoundParameters['Verbose'].IsPresent) { $Verbose=$True } Else { $Verbose=$False }#EndIf Something tells me it can be simplified but I went for the ease of readability in that example.
    – LMA1980
    Dec 22, 2017 at 14:56
  • Simplified version of my previous post:$Verbose=[bool]$PSCmdlet.MyInvocation.BoundParameters['Verbose'].IsPresent. This handle the case where Verbose is not present and IsPresent return a $null value.
    – LMA1980
    Jan 3, 2018 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.