-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Describe the bug
If a module using ScriptClass calls Enable-ScriptClassVerboseOutput, subsequent verbose output in ScriptClass methods called by that module do indeed emit verbose output. But if one of those ScriptClass methods makes a method call function in another module that itself calls into a ScriptClass method defined in that module, there is no verbose output. Essentially Enable-ScriptClassVerboseOutput is not transitive.
Workaround is to explicitly specify the -Verbose option to whatever function from the second module is invoked by the first prior to calling the second ScriptClass method.
To Reproduce
- Call
Enable-ScriptClassVerboseOutputfrom some function in module A. The function should support[cmdletbinding()]so that-verboseis a valid parameter - In that function, call a ScriptClass method defined in module A that emits verbose output
- In the method above, call a function F defined in module B that calls a ScriptClass method also defined in module B that emits verbose output
- From the cli, call the function in step 1 above with the
-verboseparameter.
Expected behavior
Expect to see the verbose output from both of the ScriptClass methods in modules A and B described in steps 2 and 3 emitted in the console
Result
Only verbose output from module A (step 2) is emitted.
Occurs on PowerShell 5 and 6 regardless of platform, was originally reproduced on PowerShell 5.1 on Windows 10.
Workaround
As suggested earlier, module A can explicitly specify the verbose parameter to function F which must also support [cmdletbinding()]. When specifying the parameter, the caller can test for whether VerbosePreference is set to Continue to create a switch parameter set to the correct value. Splatting via @ for the function call is one way to parameterize verbose:
function myFunctionF {
[cmdletbinding()]
param($param1, $param2)
$conditionallyVerboseParameter = @{
Verbose=([System.Management.Automation.SwitchParameter]::new($VerbosePreference -eq 'Continue'))
}
Invoke-MethodFromAnotherModule $param1 $param2 @conditionallyVerboseParameter
}