我希望在腳本和函數中使用Write-Verbose
命令行開關。它在腳本(.ps1)文件中按預期方式工作,但不在模塊(.psm1)文件中 - 模塊中將忽略該命令行開關。在PowerShell模塊中忽略了Write-Verbose
運行下面的腳本:
PS> .\scaffold.ps1 -verbose
產地:
VERBOSE: starting foo
path: c:\bar.txt
[missing entry here - 'verbose path: c:\bar.txt']
VERBOSE: ending foo
scaffold.ps1:
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt"
write-verbose "ending foo"
Common.psm1:
function foo {
[cmdletbinding()]
Param(
[string]$path
)
write-host "path: $path"
write-verbose "verbose path: $path"
}
在這一點上我沒有關聯一個清單(.psd1)和模塊(.psm1)。
是否有我需要使用的模塊特定語法?
**編輯**
我需要的是一種方法來確定是否-verbose
標誌已在.PS1文件中設置這樣我就可以把它傳遞給名爲.psm1文件。
scaffold.ps1:
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt" $verbose_flag # pass verbose setting to module based on what was set on the script itself
write-verbose "ending foo"
感謝分享;同意這似乎是唯一可行的;但它聞起來不好...因爲這有一個功能建議。對於PowerShell atm,MS Connect已禁用,因此現在已經發布了博文:https://developer42.wordpress.com/2017/02/04/powershell-suggestion-simplify-write-verbose-in-modules/ – JohnLBevan 2017-02-04 10:45:55
更新:記錄爲問題在PowerShell的GitHub頁面上:https://github.com/PowerShell/PowerShell/issues/3106 – JohnLBevan 2017-02-07 09:51:44