Prevent Write-Debug From Bugging You

If you are like me, you try to take advantage of already available methods of displaying output that displays information about a running function or script or  some other internal information by using Write-Verbose/Debug. The best thing about this is that it is simple to decide when to use it by specifying the various Write* commands anywhere in the code and as long as you have [cmdletbinding()] with Param() in the beginning of your code, all you have to do is use the appropriate switch (-Verbose or –Debug) and you can have that information out there to review.

The only problem that I would run into is the use of –Debug and those annoying confirmation prompts that always occur. You never see this with –Verbose and for that reason, I typically use that  so I do not get bombarded with a confirmation prompt each and every time I use it (that’s changed for me now and I will show you how it works later on). Take this example:

Function Foo {
    [cmdletbinding()]
    Param (
        [int]$Value
    )
    $iteration = 0
    1..5 | ForEach {
        $iteration++
        Write-Debug "Iteration: $iteration"
        $return = Get-Random -InputObject (1..$Value)
        Write-Debug "Return: $Return"
    }
}

This is a very simple function meant only to highlight the issue that I have been talking about. As soon as I run this function with the –Debug statement, I will see a confirmation prompt for each and every Write-Debug that exists.

image

Ok, once was enough, but this is out of hand. Even specifying “A” to continue with All operations seems not to work (there may be a logical explanation for this). Can you imagine having to do this for each and every Write-Debug call as well as accounting for each iteration that occurs? So why is this happening? Lets take a look and see what happens here.

Function Foo {
    [cmdletbinding()]
    Param (
        [int]$Value
    )
   
    $PSBoundParameters.GetEnumerator() | ForEach {
        Write-Verbose $_
    }
    Write-Verbose "DebugPreference: $DebugPreference"
 
    $iteration = 0
    1..5 | ForEach {
        $iteration++
        Write-Debug "Iteration: $iteration"
        $return = Get-Random -InputObject (1..$Value)
        Write-Debug "Return: $Return"
    }
}

I’ve added a couple of extra lines of code here to help see what happens when I run the function with the –Debug parameter. Viewing the $PSBoundParameters and $DebugPreference automatic variable will show us what is happening here.

image

I ran it first without the –Debug parameter and you can see that $DebugPreference is set to SilentlyContinue. The second run with the –Debug parameter shows a different story. We can tell in the $PSBoundParameters that it the parameter is being used, not a big thing, but looking at $DebugPreference now shows it has been set to ‘Inquire’, not the ‘Continue’ that I would have expected. Because of this, we will always receive a prompt for confirmation whenever –Debug is used.

Knowing this, I can now use a bit of code to make sure that when –Debug is used, I will not get a prompt each time it is used.

If ($PSBoundParameters['Debug']) {
    $DebugPreference = 'Continue'
}

I have the above code used at the beginning of my function so it will set the script scope of $DebugPreference to ‘Continue’ which in turn will make sure that I will not have a confirmation prompt. Let’s give it a spin and see what happens.

image

As you can see, I had 0 confirmation prompts and the script continued on showing my Debug stream. So with that, you now have an alternative to dealing with the Debug confirmation prompt the next time you decide to use Write-Debug in your code!

About Boe Prox

Microsoft Cloud and Datacenter MVP working as a SQL DBA.
This entry was posted in Debug, powershell and tagged , , . Bookmark the permalink.

One Response to Prevent Write-Debug From Bugging You

  1. Pingback: Mixed links: Powershell | PlayfulFlower

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s