Pass arguments to a scriptblock in powershell -
i guess can't this:
$servicepath = $args[0] if(test-path -path $servicepath) <-- not throw in here $block = { write-host $servicepath -foreground "magenta" if((test-path -path $servicepath)) { <-- throws here. dowork } }
so how can pass variables scriptblock $block?
keith's answer works invoke-command
, limit can't use named parameters. arguments should set using -argumentlist
parameter , should comma separated.
$sb = {param($p1,$p2) $ofs=','; "p1 $p1, p2 $p2, rest of args: $args"} invoke-command $sb -argumentlist 1,2,3,4
Comments
Post a Comment