Powershell

Table of Contents

Jobs and Scheduled Tasks

PowerShell can be used to leverage the native windows functionality of scheduling jobs and tasks Into the runtime environment that can be scheduled to perform actions at certain intervals such as Running a script or sending an email or react to triggers such as a change of state of a file that’s been Updated.

These jobs and tasks can simplify system administration by allowing jobs & tasks to perform the action Of a staff member and repeating it automatically for varied results.

Unlike scripting, jobs can perform in any order and are not dependant on the previous job completing. Subsequent Jobs can be run all at the same time and results can be retrieved at any time of completion. This is what Makes PowerShell Jobs more powerful than their scripting companions, the fact that jobs can trigger scripts, be Scheduled, and run independent of one another.

Knowing how to perform jobs and tasks makes system administrator an automated job. Advanced PowerShell users Can automate the job of multiple staff with a string of scheduled jobs and tasks to make redundant the average IT employees role and further more leveraging jobs and tasks with Azure and voice automated systems such as Azure And Alexa using Skills can have a complete voice automated workspace.

Business places that employee Amazon Alexa have created custom skills to run various PowerShell jobs and tasks on Voice command to have a complete autonomous workspace. Staff simply talk to the Alexa regarding their requests and a Alexa Skill will run background jobs and tasks that interact with PowerShell and Python to produce the meaningful results. Having an Voice automated workspace with Jobs and Tasks help reduce the need for complex process and end users.


PowerShell Jobs




Start Job

Powershell jobs are the easiest automated jobs to run. To run, use the PowerShell command
start-job
. The Start-Job command starts an PowerShell background job on the local computer. You may also target a start job using the
invoke-command
command on another device.

The below job is named using the –Name parameter for ease of retrieving job at a later stage, and the –ScriptBlock parameter contains the commands to run.

Start-Job -Name "GetProcess" -ScriptBlock {Get-Process}


A PowerShell background job runs an specified command in the backend of the session. This means that when a
start-job
command is run, the processing occurs in the backend (Runtime Environment) leaving the shell still operational to use. When a background job is started the job object returns immediately even if the job isnt complete. This is just a logical link to the job that can be retrieved at any time to see the results. There is no interuption to the shell session while jobs are running in the backend, barring complex jobs that take up vast amounts of system resources.

The job object result contains information about the job, but it doesn't contain the job results. When the job finishes, the
Receive-Job
command may be used to get the results of the job. When a job is received, the results displayed are the commands that were run within the job, whether it is to get computer info regarding remote computers, or run a copy paste of a folder into another folder.

To run a background job on a remote computer, use –AsJob- parameter that's available with many cmdlets, or use the
Invoke-Command
command to run a
Start-Job
command on a remote computer.

To Start a PowerShell job that runs a script use the –FilePath parameter e.g.

Start-Job -FilePath "c:\scripts\test_script.ps1"


To start a Job that gets the local computers list of process

Start-Job -Name GetProcess -ScriptBlock {Get-Process}




Start Job on remote computer

Invoke-Command -ComputerName "SERVER01"-ScriptBlock {Get-Service} -JobName "GetServicesRemotely"


For the official Microsoft Documentation on command "start-job" see here


Get-Job


Once a job is started the process runs in the background session of the shell environment. To get a list of all the jobs that are running on the system use the
Get-Job
command. You may use
Get-Job
command to get jobs that were started by using the Start-Job command, or by using the -AsJob parameter of any command.


Get a Job by instance ID

Get-Job -Name Job1


Get Job that includes a specific command

Get-Job -Command "*get-service*"






For more information see official Microsoft Knowledge Base documentation here Get-Job


Receive-Job

Once a Job is started in the background of the current session to retrieve the results of the Job object, the Job object results will have to be retrieved using the
Receive-Job
command. Jobs that were started with the
Start-Job
command or –AsJob parameter can be retrieved using the
Receive-Job
command.

The Receive-Job command gets the results that have been generated by the time that the Receive-Job command is submitted. If the results are not yet complete, you can run additional
Receive-Job
commands to get the remaining results.

When a Job result is received it is immediately deleted from the session unless the -Keep parameter is used with
Receive-Job
to save the results so that you can receive them again.

Receive Job and keep results

Receive-Job -Name Job1 –Keep





For more information see official Microsoft Knowledge Base documentation here Receive-Job


Stop Job

The
Stop-Job
command stops selected PowerShell background jobs that are in progress. You can use this command to stop all jobs or stop selected jobs based on their name, ID, instance ID, or state, or by passing a job object through the pipeline to the
Stop-Job
command. This command does not delete background jobs. To delete a job, use the
Remove-Job
command.

You can use
Stop-Job
to stop background jobs, such as those that were started by using the
Start-Job
commandor the -AsJob parameter of any command. When you stop a background job, PowerShell completes all tasks that are pending within that job queue and then ends the job. No new tasks are added to the queue after.

To Stop a job by Id

Stop-Job –Id 1




Stop all Jobs

Get-Job | Stop-Job


Stopping a Job on a Remote computer

$job = Invoke-Command -ComputerName "SERVER01" -ScriptBlock {Get-Process} -AsJob
$job | Stop-Job –PassThru


For more information on the Stop-job command see the official Microsoft Documentation here

Remove Job

The
Remove-Job
command deletes PowerShell background jobs. This command is used to delete all jobs or delete jobs based on their name, ID, instance ID, command, or state, or by passing a job object to
Remove-Job
. Without parameters or parameter values,
Remove-Job
has no effect.

Before deleting a running job, use the
Stop-Job
command to stop the job. If you try to delete a running job, the command fails. You can use the -Force parameter of
Remove-Job
to delete a running job. Any jobs not deleted will remain in the global job cache until you close the session in which the job was created.

To delete Job by ID

Remove-Job –Id 1




Delete all Jobs in a session

Get-Job | Remove-Job


Delete a Job on a remote computer

$job = Invoke-Command -ComputerName "SERVER01" -ScriptBlock {Get-Process} -AsJob $job | Remove-Job


For more information on the
Stop-job
command see the official Microsoft Documentation here Stop-Job


Visit next page to learn about - sessions