How to parse DCDIAG output with PowerShell

Finally added latest version to Technet Script Gallery. No need to send e-mail to me 🙂

https://gallery.technet.microsoft.com/scriptcenter/Parse-DCDIAG-with-ce430b71

All is started with the idea of creating an Active Directory Health Check script. I decided to combine couple of existing domain monitoring tools’ outputs and create one single, well designed HTML output.

At the end of the day i have following outputs:

image

image

Sorry for the pinky boxes, they are to hide customer DC names Smile

In this post i want to get into details of parsing dcdiag output with powershell. In my script, DCDIAG output  includes four columns as follows: Advertising, Replications, Services, Netlogon. It’s possible to add additional tests.

And for the passed tests rows will be green, for the failed tests rows will be red. One more thing, for the failed rows you can add hyperlinks as follow:

image

Let’s start:

As you know DCDIAG is a command line tool that analyzes the state of your domain controllers in your organization and reports for errors and warnings to assist while troubleshooting. It has couple of parameters to run across your domain controllers and DNS servers. DCDIAG can be found in Windows Server 2003 Support tools for Windows Server 2003 Servers and it is already included in Windows Server 2008 Servers.

If you have a 2008 Server which is domain joined, running DCDIAG in powershell or CMD will be enough to start tests.

For my script, i decided to execute DCDIAG script with following parameters:

Dcdiag.exe /s: DCNAME >> LOGFILE

I did not use Verbose parameter since it  is breaking down my regex function.

Here is the sample logfile of above command:

dcdiag

Very first thing is to get all DCNames in an array to pass them after s: parameter. That is pretty straight forward process.

image

AllDCs variable includes all my domain controllers in whole forest. For each DC in that array i will try to execute DCDIAG command:

image

image

Now i have a logfile output which includes DCDIAG test results for all domain controllers.

For the first column (ServerName) i create a single PSObject that will pass each DCName: (Please note i have $item variable that includes all my DCs)

image

Now i need to parse logfile not only for test names but also test results such as “failed” or “passed”.

Therefore i used following regex function to get test names and results. (I got this idea from http://www.experts-exchange.com/Programming/Languages/Scripting/Q_24712799.html Chris-Dent)

image

As you notice I used Trim() for text manipulation. I checked lines for “Starting Test: “ string and trim it. So i assigned actual test name to $TestName variable.

Then i checked for strings “passed” and “failed”. If line matches “passed” i assigned “passed” string to $teststatus variable. If not, i assigned “failed” string to same variable.

At the end of the day i will have two variables $TestStatus and $TestName which includes all test names and results foreach.

The last paragraph is to add additional value to my $results object that i created at the beginning. That means, after ServerName column, i will have additional testname columns such as Advertising, Replication, Service etc.

To format my output with HTML tags, at the beginning of script i added following lines:

image

Now comes the tough part, creating conditions to colourize rows for each result (failed & passed). Thanks to my Bermudian friend Serkan Varoglu for his great suggestions Smile

Here is how he suggest to colourize:

image

With $table variable i created tables for the tests that i want to output in HTML table:

I configured $bg parameter as $null, it will be used to configure background html tag.

I created couple of link variables, in the future what i want is to assign related technet article for each failed row. It is actually ready in script, all needed is to insert links.

And then it creates two condition for each test. If row includes “failed” string,

will be red. If not it will stay as green.

with tag, it adds related technet article links.

And the last part is to create header, close table tags and send it via a SMTP server:

image

image

In next parts, i will get into details of creating uptime, and repmon reports.

If you want the full code, just drop me an email.

Finally added latest version to Technet Script Gallery. No need to send e-mail to me 🙂

https://gallery.technet.microsoft.com/scriptcenter/Parse-DCDIAG-with-ce430b71

 

14 thoughts on “How to parse DCDIAG output with PowerShell

  1. Hi ,
    would you be kind enough to mail me the full code for the :How to parse DCDIAG output with PowerShell

  2. good code;
    would you be kind enough to mail me the full code for the :How to parse DCDIAG output with PowerShell

    thaks a lot
    regards

Comments are closed.