Suggestion :: Write-Verbose Behaviour when packaged in a module #3106

Open
JohnLBevan opened this Issue Feb 6, 2017 · 0 comments

Projects

None yet

1 participant

@JohnLBevan
JohnLBevan commented Feb 6, 2017 edited

When Write-Verbose is included in a function or cmdlet in a module file, it doesn't behave in the same way as it would had that function been written in the main script file. i.e.

# MyScript.ps1
[CmdletBinding()]
param()
 
function Invoke-Demo {
    [CmdletBinding()]
    param ([Parameter()][string]$Message)
    process {Write-Verbose $Message}
}
 
Invoke-Demo 'This DOES show when I run ".\MyScript.ps1 -Verbose"'
Write-Verbose 'This DOES show when I run ".\MyScript.ps1 -Verbose"'

Behaves differently to:

# MyModule.psm1
function Invoke-Demo {
    [CmdletBinding()]
    param ([Parameter()][string]$Message)
    process {Write-Verbose $Message}
}
# MyScript.ps1
[CmdletBinding()]
param()
Import-Module -Path '.\MyModule.psm1' -Force
Invoke-Demo 'This does NOT show when I run ".\MyScript.ps1 -Verbose"'
Write-Verbose 'This DOES show when I run ".\MyScript.ps1 -Verbose"'

This seems counter intuitive, and overly complex to get write-verbose to function based on whether the script is being run with the verbose switch enabled.


Suggested Solution
Since there may be a design reason for this behaviour, it's likely best that any new behaviour be controlled explicitly. As such I'd suggest enabling Write-Verbose to function as desired by including a switch when importing the module; i.e.

Import-Module -Path '.\MyModule.psm1' -Force -InheritVerbose

More information here: https://developer42.wordpress.com/2017/02/04/powershell-suggestion-simplify-write-verbose-in-modules/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment