Wednesday, February 4, 2015

#43 : Powershell Jobs

This is one of the complex topic and needs proper understanding before I start with how to achieve with Powershell.

PROCESS AND THREAD

A process is an instance of application which runs under its own virtual address space and runs independent of other processes.

A thread is a basic executable unit. Each process contain one or more threads running to perform specific parts of the process. A thread is scheduled on the basis of availability of resources such as CPU, Memory and others.

BENEFITS OF MULTI-THREADING

With the help of jobs, we can implement multi-threading with Powershell. It can be very much helpful for requirements like below-
1. Copying files to multiple servers parallely.
2. If the script runs on different host to collect some information, multi-threading can save a lot of time. For example, if each remote host takes 5 minutes to complete and we have 100 hosts, running them serially will take 500 seconds. Using multi-threading, you can do the same task within 5 minutes.

But we have to be sure that we have enough CPU to perform tasks in parallel. Especially file copy operations are memory consuming. So we will not throw everything in the same time, we will do the same task in sets, for example 5 servers in each set, once all of them complete, next set. Or else make sure 5 tasks to run a time, one any of the server completed, one more task will be created, but if all 5 are running, we will wait! Just thinking... Believe me there is no script written by this point of time.

DEPLOYMENT PLAN

There might be a core .NET method which might be useful in this case. I don't know, but I will deploy the same using Powershell jobs. Powershell can create, stop and check status of jobs created. The job should not be confused with SQL Server jobs or Windows Scheduler jobs. Powershell has the capability to create background jobs.

1. Each job runs under context of the same process. where it was started There is nothing like job server where you can control other jobs running on the host.

2. The failure of successs of jobs can be monitored with get-job cmdlet.

3.
start-job : Starts the job
stop-job : Stops a job
get-job : Shows the status of job
receive-job : Shows the output of command run with the job
(receive-job can be useful in some other cases to store results temporarily).

4. A job does not output anything in main console. This applies to failure of job also. The output is only received when the job completes.

EXPERIMENTING WITH JOBS
1. Start the job
start-job -Name "Testing" -ScriptBlock {get-process}

2. Monitor the status of job
get-job

3. Using Job Object
get-job has Id of each job. This Id can be achieved by running a line something like this:

$jobId = start-job -Name "Testing" -ScriptBlock {get-process}
-OR-
Start the job, run get-job to get the job id to look for.

$job = get-job -id 2

$jobId can be used further as an object for the job and you can get entire information about the job:

$jobId.Name
$jobID.Status

4. receive-job to know the results:
recieve-job -id $jobId

1 comment:

  1. Your website is very beautiful or Articles. I love it thank you for sharing for everyone. Fundamentos de ITIL

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...