简体   繁体   English

如何检查 PowerShell 开关参数是否缺失或错误

[英]How to check if a PowerShell switch parameter is absent or false

I am building a PowerShell function that builds a hash table.我正在构建一个 PowerShell function 来构建一个 hash 表。 I am looking for a way I can use a switch parameter to either be specified as absent, true or false.我正在寻找一种可以使用开关参数指定为不存在、真或假的方法。 How can I determine this?我怎样才能确定这一点?

I can resolve this by using a [boolean] parameter, but I didn't find this an elegant solution.我可以通过使用 [boolean] 参数来解决这个问题,但我没有发现这是一个优雅的解决方案。 Alternatively I could also use two switch parameters.或者,我也可以使用两个开关参数。

function Invoke-API {
    param(
        [switch]$AddHash
    )

    $requestparams = @{'header'='yes'}

    if ($AddHash) {
        $requestparams.Code = $true
    }

How would I get it to display false when false is specified and nothing when the switch parameter isn't specified?当指定 false 时如何让它显示 false 而未指定 switch 参数时什么也不显示?

To check whether a parameter was either passed in by the caller or not, inspect the $PSBoundParameters automatic variable: 要检查参数是否由调用者传入,请检查$PSBoundParameters自动变量:

if($PSBoundParameters.ContainsKey('AddHash')) {
    # switch parameter was explicitly passed by the caller
    # grab its value
    $requestparams.Code = $AddHash.IsPresent
}
else {
    # parameter was absent from the invocation, don't add it to the request 
}

If you have multiple switch parameters that you want to pass through, iterate over the entries in $PSBoundParameters and test the type of each value: 如果您想要传递多个开关参数,请迭代$PSBoundParameters的条目并测试每个值的类型:

param(
  [switch]$AddHash,
  [switch]$AddOtherStuff,
  [switch]$Yolo
)

$requestParams = @{ header = 'value' }

$PSBoundParameters.GetEnumerator() |ForEach-Object {
  $value = $_.Value
  if($value -is [switch]){
    $value = $value.IsPresent
  }

  $requestParams[$_.Key] = $value
}

You can use PSBoundParameter to check 您可以使用PSBoundParameter进行检查

PS C:\ > function test-switch {
   param (
    [switch]$there = $true
   )
   if ($PSBoundParameters.ContainsKey('there')) {
       if ($there) {
          'was passed in'
       } else {
          'set to false'
       }
   } else {
       'Not passed in'
   }
}

在此输入图像描述

If you have a parameter that can be $true , $false or unspecified, then you might not want the [Switch] parameter type because it can only be $true or $false ( $false is the same as unspecified). 如果您的参数可以是$true$false或未指定,那么您可能不需要[Switch]参数类型,因为它只能是$true$false$false与未指定相同)。 As an alternative, you can use a nullable boolean parameter. 作为替代方案,您可以使用可以为空的布尔参数。 Example: 例:

function Test-Boolean {
  param(
    [Nullable[Boolean]] $Test
  )

  if ( $Test -ne $null ) {
    if ( $Test ) {
      "You specified -Test `$true"
    }
    else {
      "You specified -Test `$false"
    }
  }
  else {
    "You did not specify -Test"
  }
}

even simpler:更简单:

function test{
 [CmdletBinding()]
  param(
  [Parameter(Mandatory=$False,Position=1)][switch]$set
  )
 write-host "commence normal operation"
 if(-not $set){"switch is not set, i execute this"}
 else{"switch is set"}
}

output enter image description here output在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM