如何将 -Verbose 传播到模块函数? [英] How to propagate -Verbose to module functions?

查看:37
本文介绍了如何将 -Verbose 传播到模块函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据答案像这个和我自己的经验,Powershell可以自动处理-Verbose(和-Debug)的传播,非常方便.但是,当我想要传播详细程度的函数在模块中时,这将停止工作.用于测试的代码:

According to answers like this one and my own experience, Powershell can take care of propagating -Verbose (and -Debug) automatically, which is very convenient. However this stops working when the functions which I want to propagate verbosity to are in a module. Code used for testing this:

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

Create a directory called Mod somewhere, suppose in c:, and add 2 files:

文件 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:

Now crate the main script, say 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'].或者还有其他更短的方法吗?如果函数的行为取决于它们是模块的一部分还是点源的,那就很麻烦了.

Why is the Write-Verbose in the module's function skipped, why does propagation not behave like it does for Show-VerbosityA? (If I just dot-source Functions.ps1 instead of importing the module, the line VERBOSE: Show-VerbosityB is Verbose is printed). I could make propagation manual by e.g. calling Show-VerbosityB -Verbose:$PSBoundParameters['Verbose']. Or are there other, preferrably shorter, ways? It is quite messy if functions behave differently depending on whether they are part of a module or dot-sourced.

推荐答案

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

The reason this is happening is because the $VerbosePreference is not propagated when the module is called. I modified your script to explicitly print the value at the same points you are outputting via Write-Verbose and Write-Output.

这篇 powershell.org 帖子 建议将此添加到模块中,这对我来说很有吸引力:

This powershell.org post proposes adding this to the module, which worked like a charm for me:

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

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

问题是在 TechNet 帖子中讨论了,带有链接到获取-CallerPreferance 函数解决了这个问题.

The issue is discussed in a TechNet post, with a link to a Get-CallerPreferance function that addresses this issue.

模块:

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

这篇关于如何将 -Verbose 传播到模块函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆