简体   繁体   English

PowerShell 脚本中的布尔参数值

[英]Boolean parameter values in a PowerShell script

I'd like to understand how a PowerShell script handles boolean parameter values.我想了解 PowerShell 脚本如何处理布尔参数值。

I have a script with 4 parameters, 2 of which are boolean variables.我有一个带有 4 个参数的脚本,其中 2 个是布尔变量。 When I run the script & set one of these parameters to $False, the script still thinks this variable is set to $True.当我运行脚本并将这些参数之一设置为 $False 时,脚本仍然认为此变量设置为 $True。

Version 1: BooleanTest.ps1 (using BOOLEAN parameters)版本 1:BooleanTest.ps1(使用 BOOLEAN 参数)

param(
    [Parameter(Mandatory=$True, Position=1)][string]$param1,
    [Parameter(Mandatory=$True, Position=2)][string]$param2,
    [Parameter(Mandatory=$True, Position=3)][bool]$param3,
    [Parameter(Mandatory=$True, Position=4)][bool]$param4
)

write-output $param1, $param2, $param3, $param4

Running this script from the command prompt returns the following:从命令提示符运行此脚本会返回以下内容:

>BooleanTest.ps1 -param1 "String1" -param2 "String2" -param3 $True -param4 $False
String1
String2
$True
$True

I've also tried adjusting this script to use switch parameters instead: 我还尝试调整此脚本以改用开关参数:

Version 2: BooleanTest.ps1 (using SWITCH parameters)版本 2:BooleanTest.ps1(使用 SWITCH 参数)

>BooleanTest.ps1 -param1 "String1" -param2 "String2" -param3:$True -param4:$False
String1
String2

IsPresent
---------
     True
    False

Running this version from the command prompt returns:从命令提示符运行此版本返回:

 >BooleanTest.ps1 -param1 "String1" -param2 "String2" -param3:$True -param4:$False String1 String2 IsPresent --------- True False

Based on these trials, I have the following questions:基于这些试验,我有以下问题:

  • In Version 1, why is the fourth variable incorrectly being returned as $True when I declared it as $False?在版本 1 中,当我将第四个变量声明为 $False 时,为什么它错误地返回为 $True?
  • In Version 2, why is the "IsPresent" list being returned?在版本 2 中,为什么返回“IsPresent”列表?
  • How does the switch statement work? switch 语句是如何工作的? After passing in the variable, do I need to run additional code to assign the True/False value, or can I use the parameter as is?传入变量后,我是否需要运行额外的代码来分配 True/False 值,还是可以按原样使用参数?

I've read various several help & troubleshooting articles, but there doesn't seem to be any clear documentation on passing boolean values to a script.我已经阅读了各种帮助和故障排除文章,但似乎没有任何关于将布尔值传递给脚本的明确文档。 I would appreciate any information that would help me understand the proper techniques to apply in this situation.我将不胜感激任何可以帮助我理解在这种情况下应用的正确技术的信息。

My System Set-Up:我的系统设置:
OS: Windows 10操作系统:Windows 10
PowerShell Version: PowerShell 7.1.5 PowerShell 版本:PowerShell 7.1.5

Version 1版本 1

This works for me.这对我有用。 I get the following output:我得到以下输出:

String1
String2
True
False

I'm not sure what's going on when you run this, but this is the output expected from what you provided.我不确定运行它时发生了什么,但这是您提供的预期输出。


Version 2版本 2

You are not using bool types, you are using switch types.您使用的不是bool类型,而是开关类型。 switch behaves much like a boolean but is not a boolean. switch行为很像布尔值,但不是布尔值。 When determining truthiness, ultimately the value of IsPresent is considered and not the default behaviors of truthiness.在确定真实性时,最终会考虑IsPresent的值,而不是真实性的默认行为。 When the switch value is $false or not provided, it evaluates to $false .switch值为$false或未提供时,它的计算结果为$false If the switch value is $true or is provided, it evaluates to $true .如果switch值为$true或已提供,则计算结果为$true

Note: I stated IsPresent is considered when comparing switch to bool .注意:我说过在比较switchbool时会考虑IsPresent This is an oversimplification and technically incorrect, though you can look at IsPresent to understand how the determination would be made.尽管您可以查看IsPresent以了解如何做出决定,但这是一种过于简单化且技术上不正确的做法。 If you want a more advanced explanation, read on.如果您想要更高级的解释,请继续阅读。

The actual reason and "secret sauce" behind the magic is because the SwitchParameter type ( [switch] is a type accelerator for SwitchParameter ) overloads the == and != comparison operators, and casting operators between bool and switch both ways.魔术背后的实际原因和“秘密武器”是因为SwitchParameter类型( [switch]SwitchParameter类型加速器重载==!=比较运算符,以及boolswitch之间的转换运算符。 These overloads allow switch and bool to functionally behave like the same type during comparisons and allows implicit conversion ( typecasting ) between the two, despite being completely different ValueTypes .这些重载允许switchbool在比较期间在功能上表现得像同一类型,并允许两者之间的隐式转换( 类型转换),尽管ValueTypes完全不同。

Most of the time the differences between bool and switch won't matter.大多数情况下, boolswitch之间的差异并不重要。 You use them the same way in comparisons 99% of the time, save for maybe code intended for debugging when you would actually need to inspect the properties of either.您在 99% 的比较中以相同的方式使用它们,除了可能用于调试的代码,当您实际上需要检查它们的属性时。

They also differ in terms of default output, when writing to the console switch has a different display-formatter , which formats the output as a table showing IsPresent by default, unlike the bool type.它们在默认输出方面也有所不同,当写入控制台时, switch具有不同的display-formatter ,它将输出格式化为默认显示IsPresent的表格,与bool类型不同。 This, however, is a very minor difference.然而,这是一个非常小的差异。

Where they mainly differ is in when and how you use them.它们的主要区别在于您何时以及如何使用它们。


When to use switch何时使用switch

switch is best for using with parameter definitions. switch最适合与参数定义一起使用。 They are idiomatic to PowerShell in this way and you don't need to provide a $true or $false value with the parameter.它们以这种方式对 PowerShell 来说是惯用的,您不需要为参数提供$true$false值。 You simply specify them or don't like so:您只需指定它们或不喜欢它们:

BooleanTest2.ps1 -param1 "String1" -param2 "String2" -param3

This will output the same as your current invocation of your version 2 boolean test.这将输出与您当前调用版本 2 布尔测试相同的输出。 Although as you discovered, you can provide the $true or $false value which is handy when you need to conditionally provide the switch based on another condition.尽管正如您所发现的,您可以提供$true$false值,这在您需要根据另一个条件有条件地提供switch时很方便。

When to use bool何时使用bool

Conversely, using bool for a parameter is less common and is not considered idiomatic.相反,对参数使用bool不太常见并且不被认为是惯用的。 You have to provide both the parameter and the value the same way you would for any other type.您必须以与任何其他类型相同的方式提供参数和值。 In practice, this generally isn't a problem as long as it's documented but it's something to consider.在实践中,只要有记录,这通常不是问题,但需要考虑。 bool is most often used anywhere else in a script you aren't defining function parameters. bool最常用于脚本中未定义函数参数的任何其他地方。

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

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