[玩转系统] PowerShell 中的强制参数
作者:精品下载站 日期:2024-12-14 05:28:39 浏览:14 分类:玩电脑
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
类型值。
将 Mandatory
与 ValidatePattern
结合使用
使用 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]+$
正则表达式,它匹配一个或多个大小写字母。这里,+
匹配一个或多个字母; ^
和 $
用于确保指定的模式匹配整个字符串,而不是子字符串。
根据需求,我们还可以使用其他验证模式,例如 ValidateCount
、ValidateLength
和 ValidateSet
等,您可以学习这里。现在,当我们想要接受给定值集中的强制参数时,还有一种可能性。在这种情况下,我们可以使用 ValidateSet 属性。让我们在下一节中学习它。
将 Mandatory
与 ValidateSet
结合使用
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 值一个强制
参数?让我们在下一节中看看如何做到这一点。
将 Mandatory
与 ValidateNotNullOrEmpty
结合使用
使用 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
值。
使用多个强制
属性
使用 n
个 Mandatory
属性使 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
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag