Skip to content

Enable-ScriptClassVerboseOutput only enables verbose in the calling module #28

@adamedx

Description

@adamedx

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

  1. Call Enable-ScriptClassVerboseOutput from some function in module A. The function should support [cmdletbinding()] so that -verbose is a valid parameter
  2. In that function, call a ScriptClass method defined in module A that emits verbose output
  3. 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
  4. From the cli, call the function in step 1 above with the -verbose parameter.

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions