我希望使用Write-Verbose
脚本和函数中的命令行开关。它在脚本 (.ps1) 文件中按预期工作,但在模块 (.psm1) 文件中则不然 — 该 commandlet 在模块中被忽略。
运行以下脚本:
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"
常见.psm1:
function foo {
[cmdletbinding()]
Param(
[string]$path
)
write-host "path: $path"
write-verbose "verbose path: $path"
}
此时,我尚未将清单 (.psd1) 与模块 (.psm1) 关联。
我需要使用特定于模块的语法吗?
** 编辑 **
我需要的是一种方法来确定-verbose
标志已在 .PS1 文件上设置,因此我可以将其传递给 .PSM1 文件。
脚手架.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"
To get Write-Verbose
模块中 cmdlet 的输出,您需要使用-verbose
公共参数。看http://technet.microsoft.com/en-us/magazine/ff677563.aspx http://technet.microsoft.com/en-us/magazine/ff677563.aspx
使用您的代码:
>import-module R:\Common.psm1
>foo "c:\users"
path: c:\users
>foo "c:\users" -verbose
path: c:\users
VERBOSE: verbose path: c:\users
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)