The Verbose & Debug Piplines & external cmdlets

by Eurisko at 2013-01-27 09:02:34

I still struggle with Scope on Powershell when it relates to variables, what gets passed, and what doesn’t.

What I’m trying to get my head around at the moment is -Verbose & -Debug

If I invoke Script A with -Debug or -Verbose, and Script A calls 3 different external cmdlets/functions, will those external cmdlets also be run in -debug or -verbose mode? Or do I need to specifiy that in Script A to pass it along if it picks it up as a parameter?
by DonJ at 2013-01-28 12:39:21
It depends a bit. When writing an advanced function or script - one with [CmdletBinding()] and a parameter block - I’ve definitely seen -verbose and -debug "pass through." This has nothing whatsoever to do with scope, though. Not really. Although you could drag scope into it by manipulating $VerbosePreference and $DebugPreference, I suppose.
by Eurisko at 2013-01-29 06:51:56
All of my advanced functions I have been writing are all configured with [CmdletBinding()] and a Param block at the top.
I invoke those functions from a script. I wasn’t sure if the script needed to call the the functions with -Verbose, or if calling the script with -Verbose was enough to pass through. Can I configure a script that isn’t a function to also support [CmdletBunding()] as well?
by DonJ at 2013-01-29 07:32:00
Yup. The attribute is legal in scripts.
by Eurisko at 2013-01-29 14:33:13
After a ton of testing, that does seem to be the case as long as I make sure all of my functions support the CmdletBinding feature. Loaded the heck out of my functions with Write-Verbose and Write-Debug output. Works like a champ!
by DonJ at 2013-01-29 14:38:45
Right. [CmdletBinding()] is what makes those common parameters all work without you needing to declare them.