using the -debug switch

In a previous post I suggested using the following code to enable a debug, but working on a function today I realised there is a better way to do this.

Here’s the old code:

if ( $DebugPreference -ne “SilentlyContinue” ) { $debug = $true }
else { $debug = $false }

Instead you can use the PSBoundParameter hashtable to discover if the debug switch was used. You still need to set the $debugPreference variable to “continue” though.

if ( $PSBoundParameters.containskey(“debug”) ){
 if ( $debug = [bool]$PSBoundParameters.item(“debug”) ) { 
  $DebugPreference = “Continue”
 }
}

Note: you also need to check if the key exists by using .containskey before getting the value else the code will throw an error. You can use this  technique to test for the other common parameters, e.g.  whatif, verbose etc.

One of the other benefits of doing it this way is that you can tell is the parameter was called like this -debug:$false and pass that to any functions or cmdlets used inside the function.

Update:  I just re-read the post http://kevsor1.wordpress.com/2011/11/03/powershell-v2-detecting-verbose-debug-and-other-bound-parameters/  that got me thinking about this and an even better solution , because it’s less lines of code is to use the following code:

if ( [bool]$debug = [bool]$PSCmdlet.MyInvocation.BoundParameters[“Debug”].IsPresent ) {
$DebugPreference = “Continue”
}

if you ever wondered why they use -eq instead of = to test equality in an if statement?  No, it’s not just to be awkward so you can put bugs in your code 🙂 this is an example of when it is useful – we test for equality and set a variable at the same time.  If $PSCmdlet.MyInvocation.BoundParameters[“Debug”].IsPresent is equal to $true then we set the variable $debug to $true and the statement returns true so the conditional code runs and $DebugPreference is set to “Continue”.

You can also add this code to your functions so that the debug status is carried through to the function – if its set at the script level then debug is enabled unless you explicitly unset it when calling the function by using -debug:$false or if debug is not enabled at the script level then you could call the function with the -debug switch to enable it for just the function.

if ( [bool]$PSCmdlet.MyInvocation.BoundParameters[“Debug”].IsPresent -eq $false ) {
$debug = $false
$DebugPreference = “SilentlyContinue”
}
elseif ( $debug = ( ( [bool]$PSCmdlet.MyInvocation.BoundParameters[“Debug”].IsPresent ) -or ( [bool]$Script:PSCmdlet.MyInvocation.BoundParameters[“Debug”].IsPresent ) ) ) {
$DebugPreference = “Continue”
}

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.