Using Powershell Jobs

By | 2023-02-13

PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework.

The Asjob is a parameter that allows you to run commands in the background as PowerShell jobs. PowerShell jobs are packages containing the results of running one or more cmdlets. You can use the Receive-Job cmdlet to get the results of PowerShell jobs.

Below are some examples of using asjob in PowerShell.

To run a script block on a remote computer as a job, you can use the Invoke-Command cmdlet with the -AsJob parameter:

Invoke-Command -ComputerName Server01 -ScriptBlock {Get-Process} -AsJob

To run a command on multiple computers as a job, you can use the -AsJob parameter with the -ComputerName parameter:

Get-Process -ComputerName Server01, Server02, Server03 -AsJob

To run a command in parallel threads as a job, you can use the ForEach-Object cmdlet with the -Parallel and -AsJob parameters:

1..10 | ForEach-Object -Parallel {Get-Random -Minimum $_ -Maximum 100} -AsJob

You can view the status of a PowerShell job by using the Get-Job cmdlet. This cmdlet returns objects that represent the background jobs that were started in the current session1. You can use the -Id, -Name, -InstanceId, -State, or -Filter parameters to get specific jobs. For example, to get the status of the job with the ID 2, you can use this command:

Get-Job -Id 2

You can also use the Receive-Job cmdlet to get the results of PowerShell jobs. This cmdlet retrieves the data that is generated by the job. You can use the -Keep parameter to keep the results in the job object for later use. For example, to get and keep the results of the job with the ID 2, you can use this command:

Receive-Job -Id 2 -Keep

There are many benefits of using jobs in PowerShell. Some of them are:

  • Improved Performance: By running multiple commands or scripts simultaneously, you can significantly improve the performance of your scripts.
  • Increased Efficiency: PowerShell jobs allow you to perform multiple tasks at once, which can significantly reduce the time it takes to complete your work.
  • Background Processing: PowerShell jobs run in the background, which means you can continue working without having to wait for them to finish.
  • Separate Results: PowerShell jobs return separate job objects, which allow you to manage the results of each job independently.
  • Status Monitoring: PowerShell jobs provide information about their state, progress, and output, which allow you to monitor the status of your jobs.
Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.