How to properly use the -verbose and -debug parameters in a custom cmdlet

前端 未结 8 671
庸人自扰
庸人自扰 2020-12-13 04:08

By default, any named function that has the [CmdletBinding()] attribute accepts the -debug and -verbose (and a few others) parameters and has the p

8条回答
  •  失恋的感觉
    2020-12-13 04:48

    I think this is the easiest way:

    Function Test {
        [CmdletBinding()]
        Param (
            [parameter(Mandatory=$False)]
            [String]$Message
        )
    
        Write-Host "This is INFO message"
    
        if ($PSBoundParameters.debug) {
            Write-Host -fore cyan "This is DEBUG message"
        }
    
        if ($PSBoundParameters.verbose) {
            Write-Host -fore green "This is VERBOSE message"
        }
    
        ""
    }
    Test -Verbose -Debug
    

提交回复
热议问题