PS1 上缺少参数参数
Missing an argument for parameter on PS1
我想弄清楚如何抛出异常或将其设置为默认值,一旦我没有在参数上放置任何值?
Function ConnectionStrings {
param(
[Parameter(
Mandatory = $false)]
[AllowNull()]
[AllowEmptyString()]
[string] $region
)
try{
if (!$regionHash.ContainsKey($region)){
$regionHash["US"]
} elseif (!$region) {
$regionHash["US"]
#$slave = $regionHash["US"].slave
#$master = $regionHash["US"].master
#$track = $regionHash["US"].tracking
} else {
$regionHash[$region]
#$slave = $regionHash[$region].slave
#$master = $regionHash[$region].master
#$track = $regionHash[$region].tracking
}
} catch {
Write-Warning -Message "OOPS!"
}
}
一旦我 运行 命令:ConnectionStrings -region
它应该抛出异常或设置为默认值。
需要你的指导,我是 powershell 的新手。
谢谢。
编辑:不设置参数值ConnectionStrings -region
您不能对同一个参数同时使用 Mandatory
和默认值。为什么不?如果 Param 具有 Mandatory` 属性,则 cmdlet 将提示用户输入值(如果未提供任何值)。
如果您想使用默认值,请提供这样的值:
function Verb-Noun
{
Param
(
# Param1 help description
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$Param1 = 'default'
)
"value of `$param1` is $Param1"
}
如果这样执行
>Verb-Noun -Param1 a
value of $param1 is a
但如果用户没有为参数提供值,将使用默认值。
Verb-Noun
value of $param1 is default
现在,如果我们像这样添加 Mandatory
属性...
Param
(
# Param1 help description
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$Param1 = 'default'
)
#rest of code...
当用户未能为 Param1
提供值时,系统会提示用户提供一个值。没有机会使用默认值。
Verb-Noun
cmdlet Verb-Noun at command pipeline position 1
Supply values for the following parameters:
Param1:
#function pauses and won't run until a value is provided
我想弄清楚如何抛出异常或将其设置为默认值,一旦我没有在参数上放置任何值?
Function ConnectionStrings {
param(
[Parameter(
Mandatory = $false)]
[AllowNull()]
[AllowEmptyString()]
[string] $region
)
try{
if (!$regionHash.ContainsKey($region)){
$regionHash["US"]
} elseif (!$region) {
$regionHash["US"]
#$slave = $regionHash["US"].slave
#$master = $regionHash["US"].master
#$track = $regionHash["US"].tracking
} else {
$regionHash[$region]
#$slave = $regionHash[$region].slave
#$master = $regionHash[$region].master
#$track = $regionHash[$region].tracking
}
} catch {
Write-Warning -Message "OOPS!"
}
}
一旦我 运行 命令:ConnectionStrings -region
它应该抛出异常或设置为默认值。
需要你的指导,我是 powershell 的新手。
谢谢。
编辑:不设置参数值ConnectionStrings -region
您不能对同一个参数同时使用 Mandatory
和默认值。为什么不?如果 Param 具有 Mandatory` 属性,则 cmdlet 将提示用户输入值(如果未提供任何值)。
如果您想使用默认值,请提供这样的值:
function Verb-Noun
{
Param
(
# Param1 help description
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$Param1 = 'default'
)
"value of `$param1` is $Param1"
}
如果这样执行
>Verb-Noun -Param1 a
value of $param1 is a
但如果用户没有为参数提供值,将使用默认值。
Verb-Noun
value of $param1 is default
现在,如果我们像这样添加 Mandatory
属性...
Param
(
# Param1 help description
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$Param1 = 'default'
)
#rest of code...
当用户未能为 Param1
提供值时,系统会提示用户提供一个值。没有机会使用默认值。
Verb-Noun
cmdlet Verb-Noun at command pipeline position 1
Supply values for the following parameters:
Param1:
#function pauses and won't run until a value is provided