Is it possible to check if -Verbose argument was given in Powershell?

前端 未结 5 1108
温柔的废话
温柔的废话 2021-02-07 02:35

I have written my own Powershell logging function Log with parameters stream (on which stream to write the message) and message (the messa

相关标签:
5条回答
  • 2021-02-07 02:36

    If you're determining whether or not to print depending on the value of the -Verbose parameter, consider using Write-Verbose instead of Write-Host: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-verbose?view=powershell-7.1

    0 讨论(0)
  • 2021-02-07 02:38

    Inside your script check this:

    $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent
    
    0 讨论(0)
  • 2021-02-07 02:40

    Also available: Check the parameter '$VerbosePreference'. If it is set to 'SilentlyContinue' then $Verbose was not given at the command line. If it is set to '$Continue' then you can assume it was set.

    Also applies to the following other common parameters:

    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... so it should be relevant with relatively old versions of Powershell. Also see "Get-Help about_CommonParameters" in Powershell v4.

    0 讨论(0)
  • 2021-02-07 02:43

    More generally: since one might specify -Verbose:$false on the command line, the following code handles that case. It also works for any other switch parameter:

    $Verbose = $false
    if ($PSBoundParameters.ContainsKey('Verbose')) { # Command line specifies -Verbose[:$false]
        $Verbose = $PsBoundParameters.Get_Item('Verbose')
    }
    
    0 讨论(0)
  • 2021-02-07 02:51

    Came across this looking for the same answer and found some good info, also some not so good. The marked answer seems to be dated and not correct as the comments stated. The PSBoundParameter property object from the MyInvocation object is a Dictionary (PoSH 5.1 up maybe earlier didn't check) which does not contain the IsPresent property. The asker also forgot to consider $VerbosePreference where other answers have presented this option.

    Here's a solution that makes it simple and easy:

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

    $PSBoundParameters is a hashtable object, if the value is present and true it will evaluate to true, if it is not present or present and not true it will evaluate to false. VerbosePreference which is set at the session level will display verbose statements when the value is Continue. Put this together in a condition using a logical or and you will get an accurate representation if verbose output is desired.

    0 讨论(0)
提交回复
热议问题