Since -ArgumentList
takes in Object[]
, I think it is received by caller
as a string. One workaround is this:
function caller ($runthis) {
$runthis = $executioncontext.InvokeCommand.NewScriptBlock($runthis)
& $runthis
}
Note that something like this works:
function caller ($runthis) {
$runthis | kill
}
$p= Get-Process -name notepad
invoke-command -computer localhost -ScriptBlock ${function:caller} -ArgumentList $p
I think scriptblocks are treated differently since it might be considered a security issue to just run them.