Try these PowerShell Start-Job examples for more efficiency

Hurry up and wait. If you’re not working with tasks in PowerShell, this can sound painfully familiar.

Many administrators who use PowerShell will eventually develop advanced scripts to handle complex tasks. While a lot of this automation work gets done quite quickly, there will be situations where a script takes a significant amount of time to complete. Watching the screen while you wait for a script to finish is not the most efficient use of time. One option is to use PowerShell tasks, which is a valuable way to run multiple scripts in parallel without hogging the console until they are complete.

How can task parallelization help? I can attest to its value through personal experience. I have a script that would take over an hour to extract the data before completing a complicated task involving multiple infrastructure components. After reworking the code to use PowerShell tasks, the script took half the time to run with the added benefit of freeing up the PowerShell console to run other commands.

What is a PowerShell background task?

The Start of employment The cmdlet starts a background task for the current PowerShell session and runs the code asynchronously. This script will run and you can continue to use PowerShell to handle other tasks. A PowerShell job is ideal for jobs that don’t require user intervention or for automated tasks that run on a schedule.

PowerShell assigns an ID to the background task. You can use the ID to check the current status of the task and get the results of all data collected by the code.

What modifications will your scripts need?

Converting your PowerShell code to run as a background task may require retooling and effort. A requirement to be aware of with PowerShell jobs is that there is no way for interactive input. The background state makes work inaccessible from the console.

When a PowerShell job runs, there is no native error reporting; the script will silently fail if problems arise. Therefore, your code will need to be examined and tested to avoid any issues that could prevent it from being executed correctly.

One way to tidy up a script is to export details or checkpoints at key parts of the script to a text file. This checks for script activity to aid in troubleshooting if an error occurs. This can be as simple as exporting a useful note such as “preprocessing completed” or “API request completed” and saving the variables. It depends on the level of information required.

How to work with PowerShell tasks

Three main cmdlets handle tasks in PowerShell: Start of employment, To get a job and Work stoppage. Running a task is pretty straightforward:

Start-Job -ScriptBlock {}

The PowerShell system runs the code in the Script block setting. A simple example is:

Start-Job -ScriptBlock {Get-Content c: mybigdatafile.txt}

This PowerShell Start-Job example demonstrates how to use the ScriptBlock parameter to run code in the background.

It is also possible to directly run a script in the background with the following PowerShell Start of employment Example:

Start-Job -FilePath c: code myscript.ps1

The script runs but cannot accept input from the console.

When running multiple background jobs, you can check the status of these jobs with the To get a job cmdlet.

As the name suggests, To get a job displays current jobs and the status of each. Even if you submit multiple jobs, they are not sequential because each Start of employment creates a child process to do the work.

Background work information
Gather data about PowerShell jobs with the Get-Job cmdlet.

The exit of To get a job The command displays the status of the script. This is invaluable when you need to know the state of a script running in the background.

Sometimes a job can go wrong or needs to be stopped. The Work stoppage The cmdlet will stop a script using the following format:

Work stopping

For more details on using these cmdlets, the PowerShell built-in help using the Acquire help command provides an abundance of documentation and sample commands.

Try these strategies to rework your code

How to develop code to use parallel processing? First, make sure the code doesn’t require interaction. You can write the script to collect data and then run the tasks with the Start of employment cmdlet.

Any required input should be provided in advance with variables or the way you collect the data, such as an API call that requires credentials for access.

Break down the code into functions. This facilitates management and integration into jobs. One way to get the output of the job is to use the transcription feature. It’s a quick way to save script output without needing to create your own report.

Using PowerShell Start of employment The cmdlet makes the script and you much more efficient. There are caveats with the code that must be self-sufficient, but it’s worth understanding how to add this flexibility to your scripts. The task management system works on most platforms that will run PowerShell, such as Linux and macOS, as well as Windows.

Source link

About Shirley L. Kreger

Check Also

5 amazing examples of augmented reality in common places

Did you know that mankind got the first impression of augmented reality in 1957? Many …