Difference between revisions of "Background Jobs (PowerShell)"

Jump to navigation Jump to search
m
Minor intro rewording, and Meta
m (→‎Overview: Minor rewording)
m (Minor intro rewording, and Meta)
Line 1: Line 1:
== Overview ==
== Overview ==
This feature allows Cmdlets to run as background jobs.  Its fairly easy to work out how to make individual Cmdlet's or PowerShell scripts to run as jobs (see [http://technet.microsoft.com/en-us/library/dd315273.aspx about_Jobs], but running functions are a bit more of a pain as the job runs in a new scope (so any functions, variables etc that are defined in your scripts scope, have no meaning in the background job's scope).  Therefore functions, etc, have to explicitly included as a script block in the background job...
This feature allows Cmdlets, script blocks, or entire scripts to run as background jobs.  Its fairly easy to work out how to make individual Cmdlet's or PowerShell scripts to run as jobs (see [http://technet.microsoft.com/en-us/library/dd315273.aspx about_Jobs], but running script blocks can be a bit more of a pain as the job runs in a new scope (so any functions, variables etc that are defined in your scripts scope, have no meaning in the background job's scope - which is a bit counter-intuitive).  Therefore functions, etc, have to explicitly included as a script block in the background job...


<source lang="powershell">
<source lang="powershell">
Line 7: Line 7:
     function TestJob {
     function TestJob {
         $processList = Get-Process
         $processList = Get-Process
         Return $processList                          # Yes could be achieved in one line, but wouldn't be much of a function!
         Return $processList                          # Yes, this could be achieved in one line, but wouldn't be much of a function!
     }
     }
}
}

Navigation menu