How do I check in dot sourced function whether $debug was specified?

This topic contains 10 replies, has 3 voices, and was last updated by Profile photo of Paul Frankovich Paul Frankovich 4 months, 2 weeks ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #38801
    Profile photo of GS
    GS
    Participant

    Hello,

    I have a 2 functions. I dot source function #1 before calling function #2. So if I called function #2 with -Debug:$true how do I find out inside function #1 that callee was run with -Debug statement?
    Checking $PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent does not work inside function #1 since it was not called directly

    #38803
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I'm not sure I follow... is function 1 calling function 2, or the other way around?

    #38804
    Profile photo of GS
    GS
    Participant

    PS1 file contains function 2, right before function 2 declaration there function 1 is dot sourced. Function 2 calls function 1.

    I call function 2 with -Debug

    #38805
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    OK. Assuming we're not talking about crossing any script module boundaries, you'd probably need to do: Get-Variable DebugPreference -Scope 1 -ValueOnly

    Check this to see if it's set to 'Continue'. -Scope 1 tells it to look up the variable in the immediate parent scope.

    #38806
    Profile photo of GS
    GS
    Participant

    Are you sure it shall be "Continue" and not "Inquire"? Right now launching with -Debug function 2 ends up with DebugPreference set to "Inquire"

    #38815
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    When I try this I do get "Inquire" if -Debug was declared on the second function but it gives me an error if -Debug wasn't declared even if I wrap it in a Try/Catch block.

    To stop the error I have to put the call for the first function in a Try/Catch which would abort execution of the first function all together.

    #38827
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Oops, you're right. It's -Verbose / $VerbosePreference that gets set to 'Continue'. 🙂

    #38834
    Profile photo of GS
    GS
    Participant

    @Paul Frankovich

    Yes but how do you check if first function run under Debug for conditional statement, $PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent will return $false

    Dave Wyatt answer is a solution, code looks like follow

    if ((Get-Variable DebugPreference -Scope 1 -ValueOnly -ErrorAction SilentlyContinue)) { Write-Verbose "Executing under debug"}
    
    #38838
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    To check if the first function is running under Debug is do:

    $PSBoundParameters.ContainsKey('Debug')

    If it returns "true" then -Debug was declared.

    #38851
    Profile photo of GS
    GS
    Participant

    @Paul, it does not work.

    
    function one
    {
    [cmdletbinding()]
    param()
    Write-Output "Function 1 Debug: "  $PSBoundParameters.ContainsKey('Debug')
    }
    
    
    function two ()
    {
    [CmdletBinding()]
    param()
    write-output "Function two debug: " $PSBoundParameters.ContainsKey('Debug')
    one 
    }
    
    
    two -Debug
    
    
    #38853
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    It's like you said up top:

    Checking $PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent does not work inside function #1 since it was not called directly

    This will only tell you if Function Two is set because Function One is not called directly. However, if you wanted different behavior out of Function One because it was called in Function Two, by checking for it in Function Two, you know that Function One will inherit it from the parent scope. If you wanted to avoid debug on Function One even if it was called on Function Two, you could do something like this:

    Function Two
    {
        [CmdletBinding()]
        Param()
    
        If ($PSBoundParameters.ContainsKey('Debug'))
        {
            One -Debug:$false
        }
        Else
        {
            One
        }
    }

    I made sure that $PSBoundParameters worked with this:

    Function Two
    {
        [CmdletBinding()]
        Param()
    
        If ($PSBoundParameters.ContainsKey('Debug'))
        {
            $true
        }
        Else
        {
            $false
        }
    }

    Here is my output:
    PS C:\Users\UID\Documents\WindowsPowerShell> two -debug
    True

    PS C:\Users\UID\Documents\WindowsPowerShell> two
    False

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.