问题描述
我希望在脚本和函数中使用 Write-Verbose
命令行开关.它在脚本 (.ps1) 文件中按预期工作,但在模块 (.psm1) 文件中无效——命令行开关在模块中被忽略.
I'm hoping to use the Write-Verbose
commandlet in scripts and functions. It works as expected in script (.ps1) files, but not in module (.psm1) files--the commandlet is ignored in modules.
运行以下脚本:
PS> .\scaffold.ps1 -verbose
产生:
VERBOSE: starting foo
path: c:\bar.txt
[missing entry here - 'verbose path: c:\bar.txt']
VERBOSE: ending foo
脚手架.ps1:
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt"
write-verbose "ending foo"
Common.psm1:
Common.psm1:
function foo {
[cmdletbinding()]
Param(
[string]$path
)
write-host "path: $path"
write-verbose "verbose path: $path"
}
此时我还没有将清单 (.psd1) 与模块 (.psm1) 相关联.
I haven't associated a manifest (.psd1) with the module (.psm1) at this point.
是否有我需要使用的特定于模块的语法?
Is there a module-specific syntax that I need to use?
** 编辑 **
我需要一种方法来确定是否在 .PS1 文件上设置了 -verbose
标志,以便我可以将其传递给 .PSM1 文件.
What I need is a way to determine if the -verbose
flag has been set on the .PS1 file so I can pass it to the .PSM1 file.
脚手架.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"
推荐答案
在这里找到答案:如何在自定义 cmdlet 中正确使用 -verbose 和 -debug 参数
脚手架.ps1:
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt" -Verbose:($PSBoundParameters['Verbose'] -eq $true)
write-verbose "ending foo"
这篇关于在 PowerShell 模块中忽略写详细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!