1

私のコマンドレットは、パイプラインから1つ、ユーザー入力から1つ、合計2つのパラメーターを取得しようとしますが、次のように位置パラメーターで実行できるのではないかと思います。

Set-login name pw | add-view viewName

add-viewは次のようになります。

[parameter(position=0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Mandatory = true)]
public loginInfo Login {}
[parameter(position=1, Mandatory = true)]
public String Name {}
...

ただし、上記のプロパティを使用すると、

Set-login name pw | add-view viewName 

Stringに変換できないというエラーが返さloginInfoれます。次のいずれかを実行する必要があります。

$a = Set-login name pw
Add-View $a viewnames

また

$a | Add-View -Name viewnames

位置パラメータとパイプラインが互いに対立しているようですが、これは本当ですか?回避策は何ですか?

ありがとう!

4

1 に答える 1

3

解決策:Nameパラメーターを最初の位置に移動して、渡された文字列がパラメーターではなくこのパラメーターにバインドされるようにします。パラメーターはLogin、とにかくパイプラインを使用して値を渡します。

理由:インラインパラメータバインディングは、パイプライン処理が開始される前に発生するプロセスです。たとえば、コマンドレットにいくぶん似ている単純な関数:

function Test-FooBar {
param (
    [Parameter(ValueFromPipeline)]
    $Foo,
    $Bar
)

process {
    "Foo: $Foo Bar: $Bar"
}
}

位置バインディングに応じて(最初の位置のパイプラインから値を取得するものを使用):

Trace-Command -Name ParameterBinding -Expression { 1 | Test-FooBar 2} -PSHost
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Foo-Bar]
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Foo-Bar]
DEBUG: ParameterBinding Information: 0 :     BIND arg [2] to parameter [Foo]
DEBUG: ParameterBinding Information: 0 :         BIND arg [2] to param [Foo] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Foo-Bar]
DEBUG: ParameterBinding Information: 0 :     BIND arg [] to parameter [Bar]
DEBUG: ParameterBinding Information: 0 :         BIND arg [] to param [Bar] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
(...)

「2」はすでに最初の位置にバインドされているため、パイプライン入力をバインドできないというエラーが発生します(最終的に)。

同じですが、今回は「2」が2番目のパラメーターにバインドされることを確認します。

Trace-Command -Name ParameterBinding -Expression { 1 | Test-FooBar -Bar 2} -PSHost
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Test-FooBar]
DEBUG: ParameterBinding Information: 0 :     BIND arg [2] to parameter [Bar]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [System.Object]
DEBUG: ParameterBinding Information: 0 :             Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 :         BIND arg [2] to param [Bar] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Test-FooBar]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Test-FooBar]
DEBUG: ParameterBinding Information: 0 :     BIND arg [] to parameter [Foo]
DEBUG: ParameterBinding Information: 0 :         BIND arg [] to param [Foo] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Test-FooBar]
DEBUG: ParameterBinding Information: 0 :     PIPELINE object TYPE = [System.Int32]
DEBUG: ParameterBinding Information: 0 :     RESTORING pipeline parameter's original values
DEBUG: ParameterBinding Information: 0 :     Parameter [Foo] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: ParameterBinding Information: 0 :     BIND arg [1] to parameter [Foo]
DEBUG: ParameterBinding Information: 0 :         BIND arg [1] to param [Foo] SUCCESSFUL
(...)

ご覧のとおり、BeginProcessingがBIND PIPELINEトリガーと呼ばれると、パイプライン入力を受け入れるパラメーターがまだ存在するため、正常に機能します。

于 2013-03-06T10:16:38.153 に答える