Run Background Jobs in Windows PowerShell
In this IT Pro Challenge, learners gain hands-on experience in Windows PowerShell to create, manage background jobs and retrieve, clear, keep, and store background job data in memory. PowerShell scripting gives Cyber Defense Analysts, Exploitation Analysts, and System Administrators essential skills in automating computer maintenance.
You will use a remote desktop protocol (RDP) to access a virtual lab machine with Windows PowerShell. If you use an Apple OS, you will need Microsoft Remote Desktop to get to the lab environment. Once in PowerShell, you will run foreground commands and then learn to use the “Start-Job” cmdlet to create background jobs. Then you will execute PowerShell commands to manage jobs and retrieve one of the background jobs. You will learn how to preserve job data without clearing its content and storing job content as a variable for future use.
This lab targets beginning learners who know how to open multiple Microsoft Windows and change directories using the Command Prompt or Powershell. Participants should plan on a full thirty to forty-five minutes to complete the lab exercises.
PowerShell provides a powerful tool with a set of default scripts called cmdlets that hook into all Windows components. Knowing how to create a PowerShell job and analyze its results forms the basis of many information technology roles. A System Administrator may need to perform automated maintenance on hundreds of servers. Using background PowerShell jobs does these kinds of tasks simultaneously and saves time in waiting for a process to stop, before starting another.
Exploitation Analysts need in-depth knowledge about Operating Systems, and creating PowerShell jobs meets this objective. Cyber Defense Analyst roles get information from PowerShell about ports and services. Software testers run Background jobs to validate hardware functionality and to automate smoke tests, identifying top concerns. So, PowerShell proves fundamental to a variety of IT tasks.
Understanding the Scenario:
You are a system administrator for a company that needs to perform automated maintenance on a large number of servers. You need to run long-running Windows PowerShell commands in the background. You use Windows PowerShell to create background jobs, and then manage jobs. Next, you retrieve the content of a job and clear the job data, and then you retrieve the content of a job and preserve the job data in memory. Finally, you store the content of a job in a variable for future use.
Configure Your Environment:
Learners log into a Microsoft Azure server with a provisioned lab virtual machine (VM). Participants must ensure that the VM displays a running status before attempting to launch it. Users initiate the VM by downloading an RDP file and then connecting to a virtual machine. Learners with an Apple system opens the VM in Microsoft Remote Desktop. Learners log into the VM, open PowerShell as an Administrator, and create a directory to save lab work. After switching to the newly created folder, participants are ready to complete the rest of the lab exercises.
Run Foreground Commands in Windows PowerShell:
You use PowerShell cmdlets to display all processes and services, including their properties, as a list. Also, you list all system event log entries. Each job may take several minutes to finish, and you can speed up this step by running a new job on a different PowerShell window or tab while the other Powershell jobs continue. You can see where running the PowerShell commands as background jobs save time and processing.
Create Background Jobs in Windows PowerShell:
In this section, you use Start-Job cmdlets to run three background jobs and view the resulting output, a table describing the job id, name, PowerShell name, and status. You see, from running a background job, the current session remains ready to add other scheduled jobs. Pressing the up arrow on the keyboard retrieves the previous PowerShell command, which you can edit with the new job name and function.
Manage Jobs by Using Windows PowerShell:
You obtain status information about all jobs. Then you stop a job and use the ‘Get-Job’ cmdlet to verify it has stopped. You learn when you quit a job, PowerShell considers all of the remaining job tasks completed and does not provide any output. Also, you find out how to know that a job has completed without interruption.
Retrieve the Content of a Job by Using Windows PowerShell:
You get the results of the completed ProcList job, executed in the “Create Background Jobs in Windows PowerShell” lab exercise. You view the outputted job on the PowerShell screen and find out that results automatically clear after receiving the job. You need to specify with PowerShell to keep the job contents.
Retrieve Job Content and Preserve the Data in Memory:
This exercise has you retrieve the third job, created in one of the previous sections while preserving the content. You rerun the same command to show that the job still outputs content. You learn that this retrieval process saves job content in a short time, but another cmdlet needs to execute to store job content for future use.
Store Job Content in a Variable for Future Use:
You assign a variable to hold the output from a background job that has completed. Then you search through the contents for all services with the string ‘Win’ in their names. You learn by running processes as background jobs; you have your PowerShell command and variable history at your fingertips. Furthermore, you can use the same PowerShell Window to execute several scripts synchronously.
Your completion of this lab has given you in-depth understanding and practice with PowerShell jobs. You have learned how to:
- Run foreground commands.
- Run background jobs.
- Manage jobs.
- Retrieve job contents.
- Retrieve job contents and preserve the data in memory.
- Store job contents to a variable.
Your hands-on experience gives you PowerShell’s advantage as a flexible scripting language to multi-task.