【问题标题】:How to mention verbose for all function call in powershell?如何在powershell中为所有函数调用提及详细信息?
【发布时间】:2018-02-21 07:42:12
【问题描述】:

我在一个模块中编写了许多 powershell 函数。 每个函数都有 Write-verbose 。

例如:

function fun1{
  # code
  Write-verbose "Useful information from fun1"
}

function fun2{
  # code
  Write-verbose "Useful information from fun2"
}

现在当我使用该函数时,我必须为每个函数调用提及详细信息。

fun1 -params <paramvalue> -verbose
fun2 -params <paramvalue> -verbose

有没有办法为所有功能全局提及 Verbose?这样我就不必为每个函数提及冗长了。

【问题讨论】:

标签: powershell powershell-3.0


【解决方案1】:

正确答案是加

$VerbosePreference = 'Continue'

在代码之前,并将其设置回

$VerbosePreference = 'SilentlyContinue'

之后。

但是,如果默认情况下您想要此信息,您可能只想将它们更改为 write-hostwrite-output

【讨论】:

    【解决方案2】:

    另一种可能性是在Write-Verbose 上添加-Verbose

    Write-verbose "Useful information from fun1" -Verbose
    

    这样做不会给调用者任何不查看信息的选项,但它会转到 Verbose 流而不是 stdout 或主机,所以如果这是您想要的,那么这可能很有用。

    【讨论】:

      【解决方案3】:

      要扩展其他两个答案,如果您希望它们默认为您详细,而不是对其他人,您可以在您的个人资料中使用 $PSDefaultParameterValues["Myfunction2:verbose"] = $true

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多