Start-Job Invoke-Command with Function and args

This topic contains 1 reply, has 1 voice, and was last updated by  eMist 2 weeks, 1 day ago.

  • Author
    Posts
  • #101995

    eMist
    Participant

    Hi Masters,

    I have just achieved on executing a function on remote machine using the code below.

    Function Foo {
    param( [String[]] $str )
    #do stuff here
    }

    Invoke-Command -ComputerName $server -ScriptBlock ${Function:Foo} -ArgumentList @(,$str)

    However, I couldn't make it work when using start-job, since I wanted this to be in background job for windows form. I am using the below that didn't work.

    $thisSB = { Invoke-Command -ComputerName $args[0] -ScriptBlock ${Function:args[1]} -ArgumentList @(,$args[2]) }
    Start-Job -Name getTempAccJob -Scriptblock $thisSB -ArgumentList $server,$thisSB,@(,$str)

    Please help, I'm totally out of resource.

    Thanks.

  • #102011

    eMist
    Participant

    After long research and not knowing everything of Invoke-Command, simply adding -Asjob will do.

You must be logged in to reply to this topic.