即, 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