解释 PowerShell 高级函数中的 Mandatory 属性。


下面我们有一个 PowerShell 高级函数的示例,我们将尝试了解强制参数是如何工作的。

function math_Operation{
   [cmdletbinding()]
   param([int]$val1,[int]$val2)
   Write-Host "Multiply : $($val1*$val2)"
   Write-Host "Addition : $($val1+$val2)"
   Write-Host "Subtraction : $($val1-$val2)"
   Write-Host "Divide : $($val1+$val2)"
}

当您执行上述示例并且没有提供值时,脚本不会提示您输入值,默认情况下它将获取值并执行程序。请参见下面的执行结果。

PS C:\WINDOWS\system32> math_Operation
Multiply : 0
Addition : 0
Subtraction : 0
Divide : 0

即使您已声明了两个变量($val1, $val2),并且您没有提供它们,该函数也不会提示您输入值,而是获取空值。若要使它们成为必需的,请使用 Mandatory **参数**,如下所示。

function math_Operation{
   [cmdletbinding()]
   param([parameter(Mandatory=$True)]
      [int]$val1,
      [Parameter(Mandatory=$True)]
      [int]$val2)
   Write-Host "Multiply : $($val1*$val2)"
   Write-Host "Addition : $($val1+$val2)"
   Write-Host "Subtraction : $($val1-$val2)"
   Write-Host "Divide : $($val1+$val2)"
}

输出

PS C:\WINDOWS\system32> math_Operation
cmdlet math_Operation at command pipeline position 1
Supply values for the following parameters:
val1: 20
val2: 10
Multiply : 200
Addition : 30
Subtraction : 10
Divide : 30

如您在输出中看到的,当您指定 Mandatory 参数时,当您在未提供值的情况下执行脚本时,它会提示您输入这些变量的值。当您不提供变量的值时会发生什么,强制参数是否接受该值?是的,它接受。它将空值作为整数变量的默认值。请参见下面的输出。

PS C:\WINDOWS\system32> math_Operation
cmdlet math_Operation at command pipeline position 1
Supply values for the following parameters:
val1:
val2:
Multiply : 0
Addition : 0
Subtraction : 0
Divide : 0

请记住,上述默认值 0 被整数单变量接受,但如果您使用整数数组 (int[]),则 PowerShell 不会接受空数组,例如,

function print_number{
   [cmdletbinding()]
   param(
      [parameter(Mandatory=$True)]
      [int[]]$numbers
   )
   Write-Output "Integer Array"
   $numbers
}

输出 

PS C:\WINDOWS\system32> print_number
cmdlet print_number at command pipeline position 1
Supply values for the following parameters:
numbers[0]:
print_number : Cannot bind argument to parameter 'numbers' because it is an
empty array.
At line:1 char:1
+ print_number
+ ~~~~~~~~~~~~
   + CategoryInfo          : InvalidData: (:) [print_number], ParameterBindin
g
   ValidationException
   + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAll
o
   wed,print_number

但是,如果您为字符串值和字符串数组提供空输入,则它们都不会接受空字符串或 null 字符串。

示例

function print_String{
   [cmdletbinding()]
   param(
      [parameter(Mandatory=$True)]
      [string]$name,
      [parameter(Mandatory=$True)]
      [string[]]$names
   )
   $name
   $names
}

输出

PS C:\WINDOWS\system32> print_String
cmdlet print_String at command pipeline position 1
Supply values for the following parameters:
name:
print_String : Cannot bind argument to parameter 'name' because it is an empty
string.
At line:1 char:1
+ print_String
+ ~~~~~~~~~~~~
   + CategoryInfo          : InvalidData: (:) [print_String], ParameterBindin
g
   ValidationException
   + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl
l
   owed,print_String

上述示例的结论是,当指定 Mandatory 参数时,null 或空值的接受取决于数据类型,并且任何集合都不会接受 null 值。

更新于: 2020年5月4日

372 次查看

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.