Running local powershell functions in a remoting session

by pradeeprawat at 2013-02-22 23:35:28

Hi Experts,

I am looking for a way or to find the possibility to run the local powershell functions in a remoting session. E.g.

function foo{
worker
}

function worker{
ps lsass
}

Invoke-Command -ComputerName myserver01 -ScriptBlock {foo}

Any help or pointers will be appreciated. Thanks in advance.
by mjolinor at 2013-02-23 10:23:18
Any functions you want to call in the script block have to be defined in the script block so they exist in the remote session.

function foo{
worker
}

function worker{
ps lsass
}

$sb=[scriptblock]::create(@"
function foo {$function:foo}
function worker {$function:worker}
foo
"@)

Invoke-Command -ComputerName myserver01 -ScriptBlock $sb
by pradeeprawat at 2013-02-23 10:38:09
Hey mjolinor, thanks that works like a charm. Totally forgot to use ScriptBlock :slight_smile: