【发布时间】: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