6
function test-scriptblock {
1..10 }
function caller ([scriptblock]$runthis) {
& $runthis
}

以下は問題なく動作します。

caller -runthis ${function:test-scriptblock}

これは機能しません

invoke-command -ComputerName localhost -ScriptBlock ${function:caller} -ArgumentList ${function:test-scriptblock}

Cannot process argument transformation on parameter 'runthis'. Cannot convert the "
1..10 " value of type "System.String" to type "System.Management.Automation.ScriptBlock".
+ CategoryInfo          : InvalidData: (:) [], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError
4

3 に答える 3

6

これが「既知の問題」であることを確認しました。ほとんどの場合、スクリプトブロックのリモート処理では、スクリプトブロックと同じように問題なく逆流しますが、逆流ArgumentListしないため、代わりに逆流します

function Caller($runthis)
{
   $runthis = [Scriptblock]::Create($runthis)
   &$runthis
}
于 2011-08-24T19:37:34.307 に答える
2

-ArgumentListを取り込むので、文字列としてObject[]受け取られると思いcallerます。1 つの回避策は次のとおりです。

function caller ($runthis) {
$runthis = $executioncontext.InvokeCommand.NewScriptBlock($runthis)
& $runthis
}

次のようなものが機能することに注意してください。

function caller ($runthis) {
$runthis  | kill
}

$p= Get-Process -name notepad
invoke-command -computer localhost -ScriptBlock ${function:caller} -ArgumentList $p

スクリプトブロックを実行するだけではセキュリティ上の問題と見なされる可能性があるため、スクリプトブロックの扱いは異なると思います。

于 2011-08-24T19:34:51.653 に答える
0

Adapted to your initial code, I do it so :

caller -runthis (get-item Function:\test-scriptblock).scriptblock

A function is not a scriptblock, a scriptblock is a property of a function.

于 2011-08-25T05:23:59.987 に答える