Posts RSS Comments RSS 253 Posts and 407 Comments till now

Breaking down DCDiag.exe to an object

Using some regex magic and some custom object mojo I threw together this DCDiag objectifier.

These are very early bits (only spent a few minutes on it) and may not go any further as I think this would be better as a cmdlet, but it was fun to play with and show some of the POWER you have at your finger tips.

The Code

$DCDiag = Get-Content c:\temp\dcdiag.txt

# Creating the DCDiag object
$DCDiagObject = "" | Select-Object Server,Advertising,SPNs,KnownRoles,Tests
$DCDiagObject.Server = ""
$DCDiagObject.Advertising = @()
$DCDiagObject.KnownRoles = @()
$DCDiagObject.SPNs = @()
$DCDiagObject.Tests = @()

# Setting up RegEXs here so they are easier to consume for the viewer
$DCNameRegex = "^\s*\* Connecting to directory service on server (?<DCName>\w+)."
$Advertising = "^\s+The\sDC\s\w*\sis\sadvertising\sas\s(a|an|having a)\s(?<Type>.*)"
$KnownRolesRegex = "^.*Role\s(?<Role>.*)\sOwner = CN=NTDS Settings,CN=(?<Holder>\w*),"
$SPNsRegex = "^\s+\* SPN found \:(?<SPN>.*)"
$StartRegex = "^\s*\.+\s(?<Target>\w+)\s(?<Result>\w+)\stest\s(?<Test>\w+)"

# Getting stuff done
switch -regex ($DCDiag)
    $DCNameRegex        {$DCDiagObject.Server = $matches.DCName}
    $Advertising        {$DCDiagObject.Advertising += $matches.Type}
    $KnownRolesRegex    {$DCDiagObject.KnownRoles += $matches.Role}
    $SPNsRegex          {$DCDiagObject.SPNs += $matches.SPN}
    $StartRegex         {
                            $myobj = "" | Select-Object Target,Test,Result
                            $myobj.Target = $matches.Target
                            $myobj.Test = $matches.Test
                            $myobj.Result = $matches.Result
                            $myobj | Add-Member -MemberType ScriptMethod -name ToString -value {$this.Test} -force
                            $DCDiagObject.Tests += $myobj

# outputting object

One Response to “Breaking down DCDiag.exe to an object”

  1. on 14 Jun 2010 at 12:32 pmSerg

    Made small change to include Target names with special characters (like dc-01) and if test result output took two lines:



Trackback this post | Feed on Comments to this post

Leave a Reply

You must be logged in to post a comment.