Start-Job Invoke-Command with Function and args

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.


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