-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Issue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-DuplicateThe issue is a duplicate.The issue is a duplicate.
Description
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 -InheritVerboseMore information here: https://developer42.wordpress.com/2017/02/04/powershell-suggestion-simplify-write-verbose-in-modules/
Adam-S-Daniel, joeypiccola, auxym, cyrossignol, mklement0 and 6 more
Metadata
Metadata
Assignees
Labels
Issue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-DuplicateThe issue is a duplicate.The issue is a duplicate.