(2.0)スクリプト:オプションは、オプションのパラメータの任意の数ですPowershellの非位置、オプションのparams
.\{script name} [options] PATH
を - に沿って考えます冗長のために '-v'の行。 PATH引数は、最後に引数が渡されたものであり、必須です。オプションと1つだけの引数を指定してスクリプトを呼び出すことができ、その引数はパスとみなされます。私は、オプションのパラメータだけを含むパラメータリストを設定することに問題がありますが、位置付けもされていません。そのよう
#param test script
Param(
$firstArg,
$secondArg,
[switch]$thirdArg,
[Parameter(ValueFromRemainingArguments = $true)]
$remainingArgs)
write-host "first arg is $firstArg"
write-host "second arg is $secondArg"
write-host "third arg is $thirdArg"
write-host "remaining: $remainingArgs"
と呼ば:
.\param-test.ps1 firstValue secondValue
スクリプトの出力:
first arg is firstValue
second arg is secondValue
third arg is False
remaining:
私がしようとして午前行動
このクイックスクリプトは、私が午前問題を示していますを作成すると、両方の引数がthから落ちますオプションのparamsを使用し、残りのArgs変数で終了します。
This question/answerは親切望ましい行動を達成するための方法を提供し、それだけであり、少なくとも1つの必須パラメータがあり、それは他の引数の前にすべてが来る場合にだけ動作するようです。
私はfirstArgは必須作ると0の位置を指定することで、この動作を示すことができます:次のように出力されている
.\param-test.ps1 firstValue secondValue
:
#param test script
Param(
[Parameter(Mandatory=$true, Position = 0)]
$firstArg,
$secondArg,
[switch]$thirdArg,
[Parameter(ValueFromRemainingArguments = $true)]
$remainingArgs)
write-host "first arg is $firstArg"
write-host "second arg is $secondArg"
write-host "third arg is $thirdArg"
write-host "remaining: $remainingArgs"
同じ入力で実行以前のように
first arg is firstValue
second arg is
third arg is False
remaining: secondValue
最初の必須の引数が割り当てられ、残っているものはすべて投げ落としますh。
質問はこれです:私はのparamsのすべてはオプションであり、そしてそれらのどれが位置されていないようなのparamsリストを設定するにはどうすればよいですか?
私がまさに必要。 _last_引数としてPosition = 0の必須argを使用できるということは私には起こりませんでした。 – Fopedush
ラトキンに感謝、私はすでにこれで1時間を燃やしていた。私は別の質問(私はあなたに信用を持って答えている)をここに持っています:http://stackoverflow.com/questions/18861701/how-do-i-force-declared-parameters-to-require-explicit- naming – ekkis