Question: I am running the following powershell command in a build step using TFS 2018.

```
Start-Job -ScriptBlock {
    Invoke-Command -FilePath \\MyServer\run.ps1 -ComputerName MyServer -ArgumentList arg1, arg2
}

```

Since I don't want the script to affect the build step it should simply fire and forget the script. Hence I am using `Start-Job`. But it seems that once the step is done the process is killed. Is there a way to maintain the process lifetime even though the build step is done or the build process is finished?

Additional information... the powershell script should run on the remote server. The script itself triggers an .exe with parameters.

Answer: 
To simply *fire and forget*, invoke the script with `Invoke-Command -AsJob`:

```
Invoke-Command -AsJob -FilePath \\MyServer\run.ps1 -ComputerName MyServer -Args arg1, arg2
Start-Sleep 1 # !! Seemingly, this is necessary, as @doorman has discovered.

```

* This should kick off the script *remotely, asynchronously*, with a job getting created in the *local* session to monitor its execution.

	+ ***Caveat***: The use of `Start-Sleep` - possibly with a *longer* wait time -
	is seemingly necessary in order for the remote process to be created before the calling script exits, but such a solution **may not be fully robust**, as there is no guaranteed timing.
* Since you're not planning to monitor the remote execution, the *local* session terminating - and along with it the monitoring job - should't matter.
When do you want the script to *stop* running? You could use a [do-while](https://blogs.technet.microsoft.com/heyscriptingguy/2014/05/05/powershell-looping-understanding-and-using-do-while/) loop and come up with a `<condition>` that meets your needs.

```
Start-Job -ScriptBlock {
    do{
        Invoke-Command -FilePath \\MyServer\run.ps1 -ComputerName MyServer -ArgumentList arg1, arg2
        Start-Sleep 2
    }while(<condition>)
}

```

Alternatively, you could use the condition `$true` so it executes forever. You will have to stop the job later in the script when you no longer need it.

```
$job = Start-Job -ScriptBlock {
    do{
        Invoke-Command -FilePath \\MyServer\run.ps1 -ComputerName MyServer -ArgumentList arg1, arg2
        Start-Sleep 2
    }while($true)
}

Stop-Job $job
Remove-Job $job

```

I've added a `Start-Sleep 2` so it doesn't lock up your CPU as no idea what the script is doing - remove if not required.