2017-07-04 82 views
2

根據回答like this one和我自己的經驗,Powershell可以自動處理--Verbose(和-Debug),這非常方便。然而,當我想傳播冗長的函數在模塊中時,這會停止工作。代碼用於測試此:如何傳播-Verbose模塊功能?

創建一個名爲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 

爲什麼在模塊的功能寫冗長跳過,爲什麼不表現的傳播像它的顯示,VerbosityA? (如果我只是使用點源Functions.ps1而不是導入模塊,則會打印VERBOSE: Show-VerbosityB is Verbose行)。我可以通過例如呼叫Show-VerbosityB -Verbose:$PSBoundParameters['Verbose']。或者還有其他的,比較短的方式?如果函數的行爲有所不同,取決於它們是模塊的一部分還是點源。

回答

3

發生這種情況的原因是因爲在調用模塊時$VerbosePreference未傳播。 我修改了腳本,以便通過Write-VerboseWrite-Output顯示您輸出的相同點處的值。

This powershell.org post建議增加這模塊,這工作就像一個魅力對我來說:

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

一位評論提到要與鏈接錯誤報告(它不存在,或者我沒有權限查看)

問題是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 
+1

謝謝,這確實是問題的答案。不幸的不是我正在希望的:]修改每個函數來處理這個是瘋狂的,所以如果我需要所有的詳細信息,我可能會採取像'$ global:VerbosePreference = [System.Management.Automation.ActionPreference] :: Continue' .. – stijn