PowerShell Jobs run in a new process. This has pros and cons which are related.
Pros:
Cons:
Start a Script Block as background job:
$job = Start-Job -ScriptBlock {Get-Process}
Start a script as background job:
$job = Start-Job -FilePath "C:\YourFolder\Script.ps1"
Start a job using Invoke-Command
on a remote machine:
$job = Invoke-Command -ComputerName "ComputerName" -ScriptBlock {Get-Service winrm} -JobName "WinRM" -ThrottleLimit 16 -AsJob
Start job as a different user (Prompts for password):
Start-Job -ScriptBlock {Get-Process} -Credential "Domain\Username"
Or
Start-Job -ScriptBlock {Get-Process} -Credential (Get-Credential)
Start job as a different user (No prompt):
$username = "Domain\Username"
$password = "password"
$secPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username, $secPassword)
Start-Job -ScriptBlock {Get-Process} -Credential $credentials
Get a list of all jobs in the current session:
Get-Job
Waiting on a job to finish before getting the result:
$job | Wait-job | Receive-Job
Timeout a job if it runs too long (10 seconds in this example)
$job | Wait-job -Timeout 10
Stopping a job (completes all tasks that are pending in that job queue before ending):
$job | Stop-Job
Remove job from current session's background jobs list:
$job | Remove-Job
Note: The following will only work on Workflow
Jobs.
Suspend a Workflow
Job (Pause):
$job | Suspend-Job
Resume a Workflow
Job:
$job | Resume-Job