即, ABC.ps1有这个如何获取PowerShell脚本中命名参数的数量?
param(
[bool]$A= $False,
[bool]$B= $False,
[bool]$C= $False
)
$count=$Args.Count
Write-Host "$count"
如果我叫它为: \ ABC.ps1 $真$ $真真正 应该显示3
这只是一种猜测,但$参数数量.Count总是为零,可能是因为它没有保存/计数命名参数。
即, ABC.ps1有这个如何获取PowerShell脚本中命名参数的数量?
param(
[bool]$A= $False,
[bool]$B= $False,
[bool]$C= $False
)
$count=$Args.Count
Write-Host "$count"
如果我叫它为: \ ABC.ps1 $真$ $真真正 应该显示3
这只是一种猜测,但$参数数量.Count总是为零,可能是因为它没有保存/计数命名参数。
命名参数的数量可从$可以得到psboundparameters
&{param(
[bool]$A= $False,
[bool]$B= $False,
[bool]$C= $False
)
$psboundparameters | ft auto
$psboundparameters.count
} $true $true $true
Key Value
--- -----
A True
B True
C True
3
$ ARG确实将只包括未绑定的参数。
$ args将保存超过指定参数计数的值的计数(未绑定参数)。如果你有三个命名参数,并发送五个参数,$ args.count将输出2
请记住,如果列出CmdletBinding属性存在,没有剩余的参数是允许的,你会得到一个错误:
function test
{
[cmdletbinding()]
param($a,$b,$c)
$a,$b,$c
}
test a b c d
test: A positional parameter cannot be found that accepts argument 'd'.
要允许剩余的参数,您将使用ValueFromRemainingArguments参数属性。现在,所有未绑定的参数将在$ C积累:
function test
{
[cmdletbinding()]
param($a,$b,[Parameter(ValueFromRemainingArguments=$true)]$c)
"`$a=$a"
"`$b=$b"
"`$c=$c"
}
test a b c d
$a=a
$b=b
$c=c d
命名帕拉姆是绑定在$psboundparameters.count
任何其他额外的参数是绑定在$args.count
通过的总的论点是($psboundparameters.count + $args.count).
测试:
param(
[bool]$A,
[bool]$B,
[bool]$C
)
$count=$Args.Count
Write-Host "$a - $b - $c - $($args[0]) - $count"
$psboundparameters.count
$args.count
叫它.\abc.ps1 $true $true $true $false
$ psboundparameters.count是我正在寻找的东西。谢谢。 – dushyantp 2012-03-22 12:20:57
$ psboundparameters.count是我一直在寻找的东西。谢谢。 – dushyantp 2012-03-22 12:20:02