Powershell - Script automation with jobs (AD Quota)

After working a while with Powershell and doing some time expensive jobs like getting the quota of all Microsoft servers I came to the point when i was looking for some parallelisation in Powershell. My problem was the quota script, that executes the following command for each file server:

1
dirquota Quota List /Remote:$server

Actually we have a couple of servers and need to get the quota from all servers to calculate the file system usage and built a daily statistic. The first script collected the data from each server. Since this is a boring job, because reading a file stream and writing it to a local file is really boring, even for a computer. I wanted to accelerate this step. So i got in touch with Powershell and jobs.

Jobs are (dont kill me) distantly related with multithreading in .NET. Indeed, multithreading in .NET has many more advantages and features than in powershell, but as a beginner (some years ago :) ) in development i didn’t do anything more than just starting threads and wait until they are finished (Yep, invokation was a foreign concept to me). So this relation is not so far out. Anyway, jobs do a great job if you have time consuming actions over and over again.

First of all I declaered a global array for my fileservers.

1
$global:DomainFileservers = @("fs1","fs2",...,"fsX")

The next step was to call the dirquota command for each server at the same time. The first problem i ran into was the parameterlist of fileservers, because a Powershell job executes only commands in a scriptblock. But after a little research i found the solution. So i defined my scriptblock like this:

1
2
3
4
5
	$GetQuotaList = {
		param($server)
		write-host "Get Quota From $server"
		dirquota Quota List /Remote:$server >> "C:\temp\Quota\data\QuotaFromQuotaList$($server).txt"
	}

As you can see, there is a param function inside the scriptblock that handles my submitted arguments. The final foreach for this example call look like that:

1
2
3
foreach ($server in $global:DomainFileservers){
	Start-Job $GetQuotaList -ArgumentList $server
}

It’s important to submit your parameter with the start-job flag “-ArgumentList”. So every scriptblock gets it’s own server parameter. Now we have to wait until every job is finished. With the command “get-job” you get every started job. The only thing we have to do here is to set a filter for the running processes within a while loop.

1
2
3
4
5
While (Get-Job -State "Running")
{
	write-host "." -nonewline
	Start-Sleep 5
}

Powershell loops here over and over again until every job has the state completed, or failed (maybe not a really good idea, but.. :) ). After all jobs are done we remove the complete joblist with the command:

1
remove-job *

The next step I’ve done was putting every quotalog into one big file, and split them into 10 equal pieces. After i had 10 quota files with the same size i started 10 powershell jobs which evaluate the data and generated me an output file for our statistic software.

As you can see, jobs are very easy to handle and can speed up your automation process extremely. I decreased the runningtime for quota evaluation process from round about 2 hours down to 25 minutes.

Here is a little statistic insight of my home directory. :)

[caption id=“attachment_946” align=“alignnone” width=“300”] Quota Statistics Quota Statistics[/caption]

Licensed under CC BY-NC-SA 4.0
Zuletzt aktualisiert am Oct 22, 2012 15:51 UTC
comments powered by Disqus
Developer / Inventor / Creator
Erstellt mit Hugo
Theme Stack gestaltet von Jimmy