Search Tools Links Login

Using Powershell Jobs


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:

About this post

Posted: 2023-02-13
By: dwirch
Viewed: 255 times

Categories

General

Tip

Tutorials

Scripting

Powershell

PowerShell Code Cache

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.