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

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

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

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

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.

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

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.

Oops, you’re right. It’s -Verbose / $VerbosePreference that gets set to ‘Continue’. :slight_smile:

@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"}

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

$PSBoundParameters.ContainsKey('Debug')

If it returns “true” then -Debug was declared.

@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

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