Zum Inhalt

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:

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.

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:

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:

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.

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:

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. 🙂

Quota Statistics
Quota Statistics
Veröffentlicht inAllgemeinBusinessCodingPowershellScripting

Schreibe den ersten Kommentar

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.