【发布时间】:2013-01-18 14:05:54
【问题描述】:
我有一个这样的脚本:
param(
[Alias('a')]
[string]$aval,
[Alias('b')]
[switch]$bval,
[Alias('c')]
[string]$cval
)
if($aval.length -gt 1)
{
Do-Something
}
elseif($bval)
{
Do-Something-Else
}
elseif($cval.length -gt 1)
{
Do-Another-Thing
}
else
{
Do-This
}
如果有人这样调用我的脚本,则会显示一个丑陋的错误,说它缺少参数“aval/bval/cval”的参数:
PS C:\> .\MyScript.ps1 -a
C:\MyScript.ps1 : Missing an argument for parameter 'aval'. Specify a
parameter of type 'System.String' and try again.
At line:1 char:18
+ .\MyScript.ps1 -n <<<<
+ CategoryInfo : InvalidArgument: (:) [MyScript.ps1], ParameterBindingException
+ FullyQualifiedErrorId : MissingArgument,MyScript.ps1
有没有办法让更清晰的,可能是一行,错误出现?另外,有没有比 elseif 语句列表更好的方法来处理参数(我的实际脚本有大约 10 个参数)?
脚本有时也会传递带有参数的参数:
EX:
PS C:\> .\MyScript.ps1 -b ServerName
感谢您的帮助!
【问题讨论】:
标签: parsing powershell parameters arguments powershell-2.0