I\'m writing a PowerShell script that will execute commands on a remote host using Invoke-Command and its -ScriptBlock
parameter. For example,
This also works:
function foo
{
param([string]$hosts, [string]$commands)
$scriptblock = $executioncontext.invokecommand.NewScriptBlock($commands)
$hosts.split(",") |% { Invoke-Command -Credential $cred -ComputerName $_.trim() -Scriptblock $scriptblock }
}
I think you want:
function Foo ( $a,$b) {
$a
$b
return "foo"
}
$x = "abc"
$y= 123
Invoke-Command -Credential $c -ComputerName $fqdn -ScriptBlock ${function:Foo} -ArgumentList $x,$y
You can wrap the functions in a block and pass the block;
$a = {
function foo{}
foo($args)
}
$a.invoke() // Locally
$rv = Invoke-Command --Credential $c --ComputerName $fqdn -ScriptBlock $a //remotely
It's hardly elegant though.