[英]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
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:基于这些试验,我有以下问题:
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
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.我不确定运行它时发生了什么,但这是您提供的预期输出。
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 comparingswitch
tobool
.注意:我说过在比较
switch
和bool
时会考虑IsPresent
。 This is an oversimplification and technically incorrect, though you can look atIsPresent
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 forSwitchParameter
) overloads the==
and!=
comparison operators, and casting operators betweenbool
andswitch
both ways.魔术背后的实际原因和“秘密武器”是因为
SwitchParameter
类型([switch]
是SwitchParameter
的类型加速器) 重载了==
和!=
比较运算符,以及bool
和switch
之间的转换运算符。 These overloads allowswitch
andbool
to functionally behave like the same type during comparisons and allows implicit conversion ( typecasting ) between the two, despite being completely different ValueTypes .这些重载允许
switch
和bool
在比较期间在功能上表现得像同一类型,并允许两者之间的隐式转换( 类型转换),尽管ValueTypes完全不同。
Most of the time the differences between bool
and switch
won't matter.大多数情况下,
bool
和switch
之间的差异并不重要。 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.它们的主要区别在于您何时以及如何使用它们。
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
时很方便。
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.