如何传播-Verbose到模块函数?

时间:2017-07-04 08:17:37

标签: powershell cmdlets

根据答案like this one和我自己的经验,Powershell可以自动处理传播-Verbose(和-Debug),这非常方便。但是,当我想要传播详细信息的函数位于模块中时,这将停止工作。用于测试此代码的代码:

在某处创建名为Mod的目录,假设在c:中,并添加2个文件:

档案c:\Mod\Functions.ps1

function Show-VerbosityB { [cmdletbinding()]Param()
  Write-Output "Show-VerbosityB called"
  Write-Verbose "Show-VerbosityB is Verbose"
}

档案c:\Mod\Mod.psd1

@{
ModuleVersion = '1.0.0.0'
NestedModules = @('Functions.ps1')
FunctionsToExport = @('*-*')
}

现在创建主脚本,说c:\Foo.ps1

Import-Module c:\Mod

function Show-VerbosityA { [cmdletbinding()]Param()
  Write-Output "Show-VerbosityA called"
  Write-Verbose "Show-VerbosityA is Verbose"
}

function Show-Verbosity { [cmdletbinding()]Param()
  Write-Output "Show-Verbosity called"
  Write-Verbose "Show-Verbosity is Verbose"
  Write-Output "Testing propagation"
  Show-VerbosityA
  Show-VerbosityB
}

Show-Verbosity -Verbose

结果

PS> . C:\Foo.ps1
Show-Verbosity called
VERBOSE: Show-Verbosity is Verbose
Testing propagation
Show-VerbosityA called
VERBOSE: Show-VerbosityA is Verbose
Show-VerbosityB called

为什么跳过模块函数中的Write-Verbose,为什么传播行为不像Show-VerbosityA那样? (如果我只是点源Functions.ps1而不是导入模块,则打印行VERBOSE: Show-VerbosityB is Verbose)。我可以通过例如传播手册致电Show-VerbosityB -Verbose:$PSBoundParameters['Verbose']。或者还有其他更好的方式吗?如果函数的行为有所不同,这取决于它们是模块的一部分还是点源的。

2 个答案:

答案 0 :(得分:5)

发生这种情况的原因是因为调用模块时$VerbosePreference不会传播。 我修改了您的脚本,以便通过Write-VerboseWrite-Output在输出的相同位置显式打印值。

This powershell.org post建议将此添加到模块中,这对我来说就像一个魅力:

if (-not $PSBoundParameters.ContainsKey('Verbose'))
{
    $VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference')
}

其中一条评论提到带链接的错误报告(它不存在或我没有权限查看)

问题是discussed in a TechNet posta link to a Get-CallerPreferance function解决了这个问题。

<强>模块:

function Show-VerbosityB { [cmdletbinding()]Param()

    <# uncomment to get verbose preference from caller, when verbose switch not explicitly used.
    if (-not $PSBoundParameters.ContainsKey('Verbose'))
    {
        $VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference')
    }
    #>

    Write-Output "`nShow-VerbosityB called"
    Write-output "Global pref: $($global:VerbosePreference)"
    Write-output "Script pref: $($script:VerbosePreference)"
    Write-output "Effect pref: $VerbosePreference"
    Write-Verbose "Show-VerbosityB is Verbose"
}

<强>号码:

Import-Module C:\Mod

Write-output "On startup: $VerbosePreference"

function Show-VerbosityA { [cmdletbinding()]Param()
  Write-Output "`nShow-VerbosityA called"
  Write-output "Global pref: $($global:VerbosePreference)"
  Write-output "Script pref: $($script:VerbosePreference)"
  Write-output "Effect pref: $VerbosePreference"
  Write-Verbose "Show-VerbosityA is Verbose"
}

function Show-Verbosity { [cmdletbinding()]Param()
  Write-Output "`nShow-Verbosity called"
  Write-output "Global pref: $($global:VerbosePreference)"
  Write-output "Script pref: $($script:VerbosePreference)"
  Write-output "Effect pref: $VerbosePreference"
  Write-Verbose "Show-Verbosity is Verbose"
  Write-Output "`nTesting propagation"
  Show-VerbosityA
  Show-VerbosityB
}

Show-Verbosity -Verbose

答案 1 :(得分:0)

如果要通过调用者脚本进行设置,请尝试以下操作:

(Get-Module'ModuleName')。SessionState.PSVariable.Set('Global:VerbosePreference',$ VerbosePreference)