【问题标题】:How to propagate -Verbose to module functions?如何将 -Verbose 传播到模块函数?
【发布时间】:2017-12-07 14:35:05
【问题描述】:

根据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']。还是有其他的,最好是更短的方法?如果函数的行为取决于它们是模块的一部分还是点源代码,这将是非常混乱的。

【问题讨论】:

    标签: powershell cmdlets


    【解决方案1】:

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

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

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

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

    问题是discussed in a TechNet post,a 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
    

    【讨论】:

    • 谢谢,这确实是答案。不幸的是,这并不是我所希望的:] 修改每个函数来处理这个问题太疯狂了,所以如果我需要所有的冗长,我可能会求助于$global:VerbosePreference = [System.Management.Automation.ActionPreference]::Continue..
    • 全局变量很难看。调用链接的Get-CallerPreferance 函数是单行的。对我来说,让我的模块函数表现得像内置 cmdlet 并且不必担心调用方站点是值得的。
    • 奇怪,这对我不起作用(Get-CallerPreference 等)。 $PSCmdlet 值存在,但是一旦我从另一个 psm1 调用一个函数,其他 psm1 中的函数就会将 $PSCmdlet 视为 null ...
    • 没关系!我的问题是我没有在我的其他 psm1 中的函数上指定 [cmdletbinding()] 参数。添加 cmdletbinding() 导致 $PSCmdlet 变量工作。
    【解决方案2】:

    如果你想从调用者脚本中设置,试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多