PowerShell 将 netdom 查询 fsmo 格式化为简单的 HTML 报告

问题描述 投票:0回答:1

大师们,我正在努力将命令 netdom query fsmo 的 PowerShell 格式设置为简单的 HTML 报告。现在,所有内容都左对齐到左列的单元格中,而我一直在努力做的是将结果放入右列的单元格中(那些是绿色的,在右边)。无论我尝试什么,所有内容都只保留在左侧列单元格中!

当前代码:

# 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>"

当前输出是什么样的: FSMO Roles

您注意到右侧的绿色小框了吗?这些就是我想要的输出!这看起来应该不难,但事实确实如此!谢谢各位高手!

powershell active-directory
1个回答
0
投票

netdom query fsmo
的输出不包含任何冒号(
:
),因此
$line -split ":"
将始终返回1个字符串。

改为分割连续的空白字符:

$fsmoRole, $fsmoServer = $line -split "\s{2,}"
© www.soinside.com 2019 - 2024. All rights reserved.