Running local powershell functions in a remoting session

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #6111

    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 🙂

You must be logged in to reply to this topic.