Search code examples
powershellactive-directory

PowerShell formatting of netdom query fsmo into a simple HTML report


Gurus, I'm struggling with PowerShell formatting of the command netdom query fsmo into a simple HTML report. Right now, everything is left justified into cells in the left column and what I have been banging my head to do is put the results into the cells in the right column (those are in Green, off to the right). No matter what I tried everything just stays in the left-hand column cells!

The current code:

# Execute 'netdom query fsmo' command
$fsmoOutput = netdom query fsmo

# Split the output into lines and remove empty lines
$fsmoLines = $fsmoOutput -split "`r?`n" | Where-Object { $_ -ne '' }

# Start adding a new table for FSMO roles
Add-Content $report "<table width='100%'>" 
Add-Content $report "<tr bgcolor='LightSteelBlue'>" 
Add-Content $report "<td colspan='2' align='center'><B>FSMO Roles</B></td>" 
Add-Content $report "</tr>" 

# Add four line breaks
Add-Content $report "`r`n`r`n`r`n`r`n"

# Iterate over each line of FSMO output to create HTML table rows
foreach ($line in $fsmoLines) {
    # Split the line into FSMO role and server
    $fsmoRole, $fsmoServer = $line -split ":"

    # Add table row with FSMO role and server
    Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor='GainsBoro' align='left'><B>$fsmoRole</B></td>"
    Add-Content $report "<td bgcolor='Aquamarine' align='left'><B>$fsmoServer</B></td>"
    Add-Content $report "</tr>"
}

# Close the table for FSMO roles
Add-Content $report "</table>"

What the current output looks like: FSMO Roles

Do you notice the small green boxes on the right hand side? Those are what I want the output in! This shouldn't seem hard, but it is! Thanks gurus!


Solution

  • The output from netdom query fsmo doesn't contain any colons (:), so $line -split ":" will always return 1 string.

    Split on successive whitespace characters instead:

    $fsmoRole, $fsmoServer = $line -split "\s{2,}"