在PowerShell模块中忽略写入详细信息
我希望在脚本和函数中使用在PowerShell模块中忽略写入详细信息,powershell,powershell-2.0,Powershell,Powershell 2.0,我希望在脚本和函数中使用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 1.ps1: [cmdle
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
1.ps1:
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt"
write-verbose "ending foo"
[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"
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt" -Verbose:($PSBoundParameters['Verbose'] -eq $true)
write-verbose "ending foo"
Common.psm1:
function foo {
[cmdletbinding()]
Param(
[string]$path
)
write-host "path: $path"
write-verbose "verbose path: $path"
}
此时,我还没有将清单(.psd1)与模块(.psm1)关联
是否需要使用特定于模块的语法
**编辑**
我需要的是一种方法来确定是否已在.PS1文件上设置了-verbose
标志,以便将其传递给.PSM1文件
1.ps1:
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt"
write-verbose "ending foo"
[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"
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt" -Verbose:($PSBoundParameters['Verbose'] -eq $true)
write-verbose "ending foo"
要从模块中的cmdlet获取
Write Verbose
输出,需要使用-Verbose
公共参数。看
使用您的代码:
>import-module R:\Common.psm1
>foo "c:\users"
path: c:\users
>foo "c:\users" -verbose
path: c:\users
VERBOSE: verbose path: c:\users
在这里找到了答案:
1.ps1:
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt"
write-verbose "ending foo"
[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"
[cmdletbinding()]
param()
import-module Common -force
write-verbose "starting foo"
foo "c:\bar.txt" -Verbose:($PSBoundParameters['Verbose'] -eq $true)
write-verbose "ending foo"
这里的问题是调用方作用域中的变量不会被脚本模块中的代码拾取。调用“\scaffold.ps1-verbose”时,$VerbosePreference在scaffold.ps1的脚本作用域中设置为“Continue”。如果从该脚本调用编译后的Cmdlet,它将接受$VerbosePreference值,但从脚本模块调用高级函数时,它们不会 我最近编写了一个函数,允许您使用$PSCmdlet和$ExecutionContext.SessionState的组合从调用方导入首选项变量,以获得适当的变量范围。在脚本模块导出函数的开头,对该命令的调用如下所示:
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
Get CallerReference函数可从下载是否在.ps1文件本身中作为变量提供了-verbose切换
foo“c:\bar.txt”$verbose
或类似的东西?从文档中我看不出来。你为什么要测试它?只需使用-Write Verbose
-如果使用-Verbose
,它将生成输出,否则它将保持沉默。-Verbose
将仅在.PS1文件中启用Write Verbose
代码。我希望有一种方法可以将此设置传递给.PSM1,而不必为.PS1文件中的每个模块调用显式写入-verbose
,因此产生了$verbose
变量的想法。我同意,这个答案确实解决了问题的一部分:“我需要的是一种方法来确定是否在.PS1文件上设置了-verbose标志,以便将其传递给.PSM1文件。“谢谢分享;同意这似乎是唯一有效的方法;但是闻起来很难闻。。。因此,我们有一个特征建议。MS Connect对于PowerShell atm是禁用的,所以现在我们已经在博客上写了:更新:在PowerShell的GitHub页面上作为一个问题记录:伟大而干净的解决方案!