当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 中的强制参数

作者:精品下载站 日期:2024-12-14 05:28:39 浏览:14 分类:玩电脑

PowerShell 中的强制参数


[玩转系统] PowerShell 中的强制参数

使用 Mandatory 属性

使用 Mandatory 属性使参数在 PowerShell 中成为强制参数。

使用强制属性:

function DisplayName {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Name
    )
    Write-Host $Name
}
DisplayName "John"

输出 :

John

首先,我们定义了一个名为 DisplayName 的函数,前面带有 function 关键字。在这个函数中,我们使用了 param 关键字(也称为 param() 块),它用于定义 $Name 参数;该参数的数据类型为string

param() 块还包含 [Parameter()] 属性,表示 $Name 参数是强制性的,不能省略;这是因为我们将 Mandatory 属性设置为 $true。在上面的代码中,$Name参数的声明具有以下特点:

  • 它从 PowerShell 控制台获取输入。
  • 它是一个强制(必需)参数。
  • 它采用一个 string 类型值。

接下来,我们使用 Write-Host cmdlet 打印 $Name 参数的值,如上面的输出所示。最后,我们通过提供 "John" 作为函数的参数来调用 DisplayName 方法。

请记住,参数和参数之间有明显的区别;参数是变量的名称,而参数是该变量包含的值。因此,在上面的示例中,$Name 是参数,而 "John" 是参数。

如果我们调用 DisplayName 函数而不提供 $Name 参数的值,会发生什么情况?让我们使用以下示例来实现这一点。

使用强制属性:

function DisplayName {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Name
    )
    Write-Host $Name
}
DisplayName

错误 :

cmdlet DisplayName at command pipeline position 1
Supply values for the following parameters:
Name:

这次,我们得到了上述错误,因为我们调用了 DisplayName 函数,但没有提供必需的 $Name 参数值。

现在,作为程序用户思考并做一些意想不到的事情,例如传递一个整数类型值(如以下代码片段所示)并观察结果。

使用强制属性:

function DisplayName {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Name
    )
    Write-Host $Name
}
DisplayName 123

输出 :

123

上面的代码执行成功了,但是应该不起作用。为什么?代码运行是因为我们提供了存储在 $Name 参数中的强制参数,并且其数据类型被决定为 param() 中的 string堵塞。因此,之后的所有内容都不会产生任何错误,但我们希望如果提供了除字母之外的任何内容,则该代码不会运行。请记住,数字不能是名称。

这意味着我们必须进行一些验证以避免语义错误(这些是代码的问题,它不会产生任何错误,但不会执行正确的操作)。请参阅以下部分,了解如何验证和仅接受 string 类型值。

MandatoryValidatePattern 结合使用

使用 Mandatory 属性和 ValidatePattern 属性来定义正则表达式来验证提供的参数。

验证强制参数的模式:

function DisplayName {
    param(
        [Parameter(Mandatory=$true)]
        [ValidatePattern('^[a-zA-Z]+$')]
        [string]$Name
    )
    Write-Host $Name    
}
DisplayName 123
DisplayName "John"

第一次函数调用引起的错误:

DisplayName: Cannot validate argument on parameter 'Name'. The argument "123" does not match the "^[a-zA-Z]+$" pattern. Supply an argument that matches "^[a-zA-Z]+$" and try the command again.

第二个函数调用的输出:

John

在这里,我们调用了 DisplayName 函数两次;对于第一次调用,我们传递了一个整数值 123 作为参数,对于第二次调用,我们指定了一个 string 类型值,"John".不幸的是,对于第一次调用,上面的代码显示了一个错误,指出它无法验证指定参数的参数。相比之下,它对于第二个函数调用工作得很好。为什么?

这是因为我们使用了[ValidatePattern()]属性来验证输入值。该属性采用正则表达式来验证提供的参数。在上面的例子中,我们使用了 ^[a-zA-Z]+$ 正则表达式,它匹配一个或多个大小写字母。这里,+匹配一个或多个字母; ^$ 用于确保指定的模式匹配整个字符串,而不是子字符串。

根据需求,我们还可以使用其他验证模式,例如 ValidateCountValidateLengthValidateSet 等,您可以学习这里。现在,当我们想要接受给定值集中的强制参数时,还有一种可能性。在这种情况下,我们可以使用 ValidateSet 属性。让我们在下一节中学习它。

MandatoryValidateSet 结合使用

Mandatory 属性与 ValidateSet 属性一起使用来验证提供的参数。

验证强制参数:

function DisplayName {
    param(
        [Parameter(Mandatory=$true)]
        [ValidatePattern('^[a-zA-Z]+$')]
        [ValidateSet('John', 'Mary')]
        [string]$Name
    )
    Write-Host $Name    
}
DisplayName "Mary"
DisplayName "John"
DisplayName "Martin"

第一个函数调用的输出:

Mary

第二个函数调用的输出:

John

第三个函数调用错误:

DisplayName: Cannot validate argument on parameter 'Name'. The argument "Martin" does not belong to the set
"John, Mary" is specified by the ValidateSet attribute. Supply an argument in the set and then try the command
again.

在这里,我们验证了模式并检查提供的参数是否属于定义的集合。 为此,我们使用 [ValidateSet()] 来提及参数参数的有效值,这意味着上述程序只有在获得 "Mary"“John” 作为参数;否则,它将生成一个错误,指出无法验证该参数。请参阅以上所有三个函数调用的输出。

到目前为止,我们已经通过指定字符串值和数字进行了学习,但是如果我们为 Mandatory 提供空字符串 ("") 或 $null 值会怎样? 参数;在这种情况下,如果我们传递空字符串或 $null 值作为参数。

为什么?这是因为 ValidateSet 会检查参数是否属于该集合。同时,ValidatePattern 会尝试匹配输入参数,如果出现任何问题,两者都会生成错误。

现在,如果我们不使用任何这些属性,既不使用 ValidateSet 也不使用 ValidatePattern ,我们如何检测空字符串或 $null 值一个强制参数?让我们在下一节中看看如何做到这一点。

MandatoryValidateNotNullOrEmpty 结合使用

使用 Mandatory 属性和 ValidateNotNullOrEmpty 属性来验证提供的参数是否既不是空字符串也不是 $null 值。

验证强制参数:

function DisplayName {
    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name
    )
    Write-Host $Name    
}
DisplayName "John"
DisplayName ""
DisplayName $null

第一个函数调用的输出:

John

第二个函数调用错误:

DisplayName: Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is
not null or empty, and then try the command again.

第三个函数调用错误:

DisplayName: Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is
not null or empty, and then try the command again.

由于 ValidateNotNullOrEmpty() 属性(用于标识空字符串和 $null),我们在第二个和第三个函数调用中遇到了错误。

如果您指定 null"null" 而不是 $null,上面的代码不会显示任何错误,因为它会处理 null 作为 string 类型值。因此,我们必须使用 ValidatePattern 属性来处理这种情况,如下所示。

验证强制参数:

function DisplayName {
    param(
        [Parameter(Mandatory=$true)]
        [ValidatePattern('^(?!null$)[a-zA-Z]+$')]
        [string]$Name
    )
    Write-Host $Name    
}
DisplayName "John"
DisplayName ""
DisplayName "null"
DisplayName null
DispalyName $null

第一个函数调用的输出:

John

第二个函数调用错误:

DisplayName: Cannot validate argument on parameter 'Name'. The argument "" does not match the "^(?!null$)[a-zA-Z]+$"
pattern. Supply an argument that matches "^(?!null$)[a-zA-Z]+$" and try the command again.

第三个函数调用错误:

DisplayName: Cannot validate argument on parameter 'Name'. The argument "null" does not match the
"^(?!null$)[a-zA-Z]+$" pattern. Supply an argument that matches "^(?!null$)[a-zA-Z]+$" and try the command again.

第四个函数调用错误:

DisplayName: Cannot validate argument on parameter 'Name'. The argument "null" does not match the
"^(?!null$)[a-zA-Z]+$" pattern. Supply an argument that matches "^(?!null$)[a-zA-Z]+$" and try the command again.

第五次函数调用错误:

DisplayName: Cannot validate argument on parameter 'Name'. The argument "" does not match the "^(?!null$)[a-zA-Z]+$"
pattern. Supply an argument that matches "^(?!null$)[a-zA-Z]+$" and try the command again.

我们在第二个和第五个函数调用时收到错误,因为空字符串和 $null 变量与提供的正则表达式不匹配。相反,第三个和第四个函数调用显示错误,因为上面的代码接受所有字母组合,不包括 null 值。

使用多个强制属性

使用 nMandatory 属性使 n 个参数在 PowerShell 中成为强制参数。

使用多个强制属性:

function DisplayName {
    param(
        [Parameter(Mandatory=$true)]
        [string]$FirstName,
        [Parameter(Mandatory=$true)]
        [string]$LastName
    )
    Write-Host $FirstName,$LastName
}
DisplayName "John" "Purell"

输出 :

John Purell

要强制使用 n 个参数,我们需要在声明参数名称之前使用 [Parameter(Mandatory=$true)],如上面的示例所示。另一种情况可能是有一个强制参数和一个可选参数。在这种情况下,我们需要省略要设为可选的参数的 Mandatory 属性。请参阅以下示例。

使用强制和可选属性:

function DisplayName {
    param(
        [Parameter(Mandatory=$true)]
        [string]$FirstName,
        [Parameter()]
        [string]$LastName
    )
    Write-Host $FirstName,$LastName
}
DisplayName "John" "Purell"
DisplayName "John"

输出 :

John Purell
John

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯