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/
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.
Behaves differently to:
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-Verboseto function as desired by including a switch when importing the module; i.e.More information here: https://developer42.wordpress.com/2017/02/04/powershell-suggestion-simplify-write-verbose-in-modules/