根據回答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']
。或者還有其他的,比較短的方式?如果函數的行爲有所不同,取決於它們是模塊的一部分還是點源。
謝謝,這確實是問題的答案。不幸的不是我正在希望的:]修改每個函數來處理這個是瘋狂的,所以如果我需要所有的詳細信息,我可能會採取像'$ global:VerbosePreference = [System.Management.Automation.ActionPreference] :: Continue' .. – stijn