我有以下(部分)脚本,它获取AD复制状态的内容并将其格式化为表格,然后通过电子邮件发送。
我的问题是:我想创建一个动态的HTML表格,就像我想要的输出。我们怎么做呢?
如果最后结果等于0,我希望该单元格的颜色为绿色,如果大于0,则应为红色。
我想要的输出:
下面是我的脚本:
$style = @"
<style>
body, table {font-family: sans-serif; font-size: 11pt; color: #1F497D;}
table {border: 1px solid black; border-collapse: collapse; color: #000000;}
th {border: 1px solid black; background: #dddddd; padding: 3px;}
td {border: 1px solid black; padding: 3px;}
</style>
"@
$mailTemplate = @"
<html><head>{0}</head><body>
This an automated message.<br />
{1}
Please review the attachment.<br /><br />Thank you.
</body></html>
"@
$DCs = Get-ADDomainController -Filter * |sort name
$results = @()
ForEach ($DC in $DCs) {
$ReplStatuses = Get-ADReplicationPartnerMetadata -target $DC.HostName -PartnerType Both -ErrorAction SilentlyContinue
If ($ReplStatuses) {
ForEach ($ReplStatus in $ReplStatuses) {
$Partner = $ReplStatus.Partner.Split(",")[1].Replace("CN=","")
$results += [pscustomobject] @{
'Source DC' = $DC.HostName.ToUpper()
'Partner DC' = (Get-ADComputer $Partner).DNSHostName.ToUpper()
Direction = $ReplStatus.PartnerType
Type = $ReplStatus.IntersiteTransportType
'Last Attempt' = $ReplStatus.LastReplicationAttempt
'Last Success' = $ReplStatus.LastReplicationSuccess
'Last Result' = $ReplStatus.LastReplicationResult
}
}
} Else {
$results += [pscustomobject] @{
'Source DC' = $DC.HostName.ToUpper()
'Partner DC' = "N/A"
Direction = "N/A"
Type = "N/A"
'Last Attempt' = "N/A"
'Last Success' = "N/A"
'Last Result' = "N/A"
}
}
}
$table = ($results | ConvertTo-Html -As Table -Fragment) -join [environment]::NewLine
$mailParams = @{
To = 'user@contoso.com'
From = 'user@contoso.com'
Subject = 'AD Status'
Body = $mailTemplate -f $style , $table
BodyAsHtml = $true
Priority = 'High'
SmtpServer = ''
Encoding = 'UTF8'
}
Send-MailMessage @mailParams
编辑1:
if ($column -eq 'Last Result' -and $val -match '^\d+$') {
# special case, if column is 'Last Result' and $val is all numeric
# and $val equals 0, use backcolor green else red
$resultColor = if ([int]$val -eq 0) { '#92D050' } else { '#FF4606' }
$td = '<td style="background-color: {0}; text-align: right;">{1}</td>' -f $resultColor, $val
}
elseif $column -eq 'Last Result1' -and $val -match '^\d+$') {
# special case, if column is 'Last Result1' and $val is all numeric
# and $val equals 0, use backcolor green else red
$resultColor = if ([int]$val -eq 0) { '#92D050' } else { '#FF4606' }
$td = '<td style="background-color: {0}; text-align: right;">{1}</td>' -f $resultColor, $val
}
1条答案
按热度按时间ubby3x7f1#
我想我会使用自己的自定义函数将$results中的数据转换为HTML表格。
我尝试模仿示例图像中的样式,并对代码做了一些小的调整
使用伪数据时,该表应如下所示: