Chapter 12. Multitasking with background jobs

 

Everyone’s always telling you to “multitask,” right? Why shouldn’t PowerShell help you out with that by doing more than one thing at a time? It turns out that PowerShell can do exactly that, especially for longer-running tasks that might involve multiple target computers. Make sure you’ve read chapters 10 and 11 before you dive into this chapter, because we’re going to take those remoting and WMI concepts a bit further.

12.1. Making PowerShell do multiple things at the same time

You should think of PowerShell as a single-threaded application, meaning that it can only do one thing at once. You type a command, you hit Return, and the shell waits while that command executes. You can’t start running a second command until the first finishes.

With its background jobs functionality, however, PowerShell has the ability to move a command onto a separate background thread (actually a separate, background PowerShell process). That enables the command to run in the background, while you continue using the shell for something else.

You have to make that decision before running the command; after you press Return, you can’t decide to move a long-running command into the background. After commands are in the background, PowerShell provides mechanisms to check on their status, retrieve any results, and so forth.

12.2. Synchronous versus asynchronous

12.3. Creating a local job

12.4. WMI, as a job

12.5. Remoting, as a job

12.6. Getting job results

12.7. Working with child jobs

12.8. Commands for managing jobs

12.9. Common points of confusion