【问题标题】:Is it possible to check if -Verbose argument was given in Powershell?是否可以检查 Powershell 中是否给出了 -Verbose 参数?
【发布时间】:2014-06-27 07:51:00
【问题描述】:

我已经编写了自己的 Powershell 日志记录函数 Log,其参数为 stream(要在哪个流上写入消息)和 message(要写入的消息)。

我的想法是我可以将输出写入控制台和日志文件。我在函数中所做的基本上是确定在哪个流上发布消息(使用 switch 语句),然后将消息写入流和日志文件:

switch ($stream) {
    Verbose {
        Write-Output "$logDate [VERBOSE] $message" | Out-File -FilePath $sgLogFileName -Append
        Write-Verbose $message
        break
    }
}

现在的问题是,是否可以检查是否给出了 -Verbose 参数?

目标是仅在给出 -Verbose 时才将消息写入日志文件。

我已经查看了以下帮助文档,但没有发现任何有用的信息:
- 帮助 about_Parameters
- 帮助 about_commonparameters

此外,-WhatIf 参数不适用于 Write-Verbose。

非常感谢您的回答!

【问题讨论】:

    标签: powershell


    【解决方案1】:

    在你的脚本里面检查这个:

    $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent
    

    【讨论】:

    • 完美,这正是我一直在寻找的!谢谢!
    • @dwettstein 很高兴为您提供帮助!
    • 这会产生“无法索引到空数组”错误。
    • 这确实正确考虑了使用-Verbose:$false 的情况。用一个简单的函数测试表明在这种情况下IsPresent 将被设置为False
    • 我在这个对象上找不到“属性 'IsPresent”。验证属性是否存在”。
    【解决方案2】:

    也可用:检查参数“$VerbosePreference”。如果将其设置为“SilentlyContinue”,则不会在命令行中给出 $Verbose。如果它设置为“$Continue”,那么您可以假设它已设置。

    也适用于以下其他常用参数:

    Name                           Value
    ----                           -----
    DebugPreference                SilentlyContinue
    VerbosePreference              SilentlyContinue
    ProgressPreference             Continue
    ErrorActionPreference          Continue
    WhatIfPreference               0
    WarningPreference              Continue
    ConfirmPreference              High
    

    Taken from an MSDN blog page from long ago... 所以它应该与相对旧版本的 Powershell 相关。另请参阅 Powershell v4 中的“Get-Help about_CommonParameters”。

    【讨论】:

    • 我就是这样选择的:$verbose = $VerbosePreference -ne 'SilentlyContinue'
    【解决方案3】:

    更一般地说:因为可能会在命令行上指定 -Verbose:$false,所以下面的代码会处理这种情况。它也适用于任何其他开关参数:

    $Verbose = $false
    if ($PSBoundParameters.ContainsKey('Verbose')) { # Command line specifies -Verbose[:$false]
        $Verbose = $PsBoundParameters.Get_Item('Verbose')
    }
    

    【讨论】:

    • 这里可能缺少一个终止的}
    【解决方案4】:

    遇到这个寻找相同的答案,发现了一些很好的信息,也有些不太好。 正如 cmets 所说,标记的答案似乎已经过时并且不正确。 MyInvocation 对象中的 PSBoundParameter 属性对象是不包含 IsPresent 属性的 Dictionary(PoSH 5.1 可能更早之前没有检查)。提问者也忘记考虑 $VerbosePreference 其他答案提供此选项的地方。

    这里有一个简单易行的解决方案:

    if ( $PSBoundParameters['Verbose'] -or $VerbosePreference -eq 'Continue' ) {
       # do something
    }
    

    $PSBoundParameters 是一个哈希表对象,如果该值存在且为 true,它将评估为 true,如果它不存在或存在且不为 true,则评估为 false。在会话级别设置的 VerbosePreference 将在值为 Continue 时显示详细语句。使用逻辑或将其放在一个条件中,如果需要详细输出,您将获得准确的表示。

    【讨论】:

      【解决方案5】:

      如果您根据-Verbose 参数的值确定是否打印,请考虑使用Write-Verbose 而不是Write-Hosthttps://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-verbose?view=powershell-7.1

      【讨论】:

        猜你喜欢
        • 2013-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 2022-01-01
        相关资源
        最近更新 更多