如何在 PowerShell 中强制设置参数?
How do I make parameters mandatory in PowerShell?
您可以在每个参数上方的属性中指定它,如下所示:
function Do-Something{ [CmdletBinding()] param( [Parameter(Position=0,mandatory=$true)] [string] $aMandatoryParam, [Parameter(Position=1,mandatory=$true)] [string] $anotherMandatoryParam) process{ ... } }
You specify it in an attribute above each parameter like this:
要使参数成为强制参数,请在参数描述中添加“Mandatory=$true”。要使参数可选,只需保留“强制”语句即可。
此代码适用于脚本和函数参数:
[CmdletBinding()] param( [Parameter(Mandatory=$true)] [String]$aMandatoryParameter, [String]$nonMandatoryParameter, [Parameter(Mandatory=$true)] [String]$anotherMandatoryParameter )
确保“param”语句是脚本或函数中的第一个语句(注释和空行除外)。
您可以使用“Get-Help”cmdlet 来验证参数是否已正确定义:
PS C:\> get-help Script.ps1 -full [...] PARAMETERS -aMandatoryParameter <String> Required? true Position? 1 Default value Accept pipeline input? false Accept wildcard characters? -NonMandatoryParameter <String> Required? false Position? 2 Default value Accept pipeline input? false Accept wildcard characters? -anotherMandatoryParameter <String> Required? true Position? 3 Default value Accept pipeline input? false Accept wildcard characters?
To make a parameter mandatory add a "Mandatory=$true" to the parameter description. To make a parameter optional just leave the "Mandatory" statement out.
This code works for both script and function parameters:
Make sure the "param" statement is the first one (except for comments and blank lines) in either the script or the function.
You can use the "Get-Help" cmdlet to verify the parameters have been defined correctly:
只是想发布另一个解决方案,因为我发现 param(...) 块有点难看。看起来像这样的代码:
function do-something { param( [Parameter(Position=0, Mandatory=$true)] [string] $First, [Parameter(Position=1, Mandatory=$true)] [string] $Second ) ... }
也可以像这样写得更简洁:
function do-something ( [Parameter(Mandatory)] [string] $First, [Parameter(Mandatory)] [string] $Second ) { ... }
看起来好多了! =$true 可以省略,因为 mandatory 是一个开关参数。
=$true
mandatory
just wanted to post another solution, since i find param(...) blocks kind of ugly.looks like this code:
param(...)
can also be written more concisely like this:
which looks much nicer! the =$true can be omitted because mandatory is a switch parameter.
您不必指定 Mandatory=true,Mandatory 就足够了。
Mandatory=true
Mandatory
简单示例:
function New-File { param( [Parameter(Mandatory)][string]$FileName ) New-Item -ItemType File ".\$FileName" }
You don't have to specify Mandatory=true, Mandatory is enough.
Simple Example:
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(4)
您可以在每个参数上方的属性中指定它,如下所示:
You specify it in an attribute above each parameter like this:
要使参数成为强制参数,请在参数描述中添加“Mandatory=$true”。要使参数可选,只需保留“强制”语句即可。
此代码适用于脚本和函数参数:
确保“param”语句是脚本或函数中的第一个语句(注释和空行除外)。
您可以使用“Get-Help”cmdlet 来验证参数是否已正确定义:
To make a parameter mandatory add a "Mandatory=$true" to the parameter description. To make a parameter optional just leave the "Mandatory" statement out.
This code works for both script and function parameters:
Make sure the "param" statement is the first one (except for comments and blank lines) in either the script or the function.
You can use the "Get-Help" cmdlet to verify the parameters have been defined correctly:
只是想发布另一个解决方案,因为我发现 param(...) 块有点难看。
看起来像这样的代码:
也可以像这样写得更简洁:
看起来好多了!
=$true
可以省略,因为mandatory
是一个开关参数。just wanted to post another solution, since i find
param(...)
blocks kind of ugly.looks like this code:
can also be written more concisely like this:
which looks much nicer! the
=$true
can be omitted becausemandatory
is a switch parameter.您不必指定
Mandatory=true
,Mandatory
就足够了。简单示例:
You don't have to specify
Mandatory=true
,Mandatory
is enough.Simple Example: