在前一个技能中我们开始将 PowerShell 的结果转换为 HTML 报告。现在,这份报告需要一些头部和尾部。以下是我们上一个版本的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
$Path = "$env:temp\eventreport.htm" $startDate = (Get-Date).AddHours(-48)
$replacementStrings = @{ Name = 'ReplacementStrings' Expression = { $_.ReplacementStrings -join ',' } }
Get-EventLog -LogName System -EntryType Error -After $startDate | Select-Object -Property EventId, Message, Source, InstanceId, TimeGenerated, $ReplacementStrings, UserName | ConvertTo-Html | Set-Content -Path $Path
Invoke-Item -Path $Path
|
要在数据前后加入内容,请使用 -PreContent
和 -PostContent
参数。比如在头部加入机器名,在尾部加入版权信息,请使用以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
$Path = "$env:temp\eventreport.htm" $today = Get-Date $startDate = $today.AddHours(-48) $startText = $startDate.ToString('MMMM dd yyyy, HH:ss') $endText = $today.ToString('MMMM dd yyyy, HH:ss')
$preContent = "<h1>$env:computername</h1> <h3>Error Events from $startText until $endText</h3> " $postContent = "<p><i>(C) 2017 SysAdmin $today</i></p>"
$replacementStrings = @{ Name = 'ReplacementStrings' Expression = { $_.ReplacementStrings -join ',' } }
Get-EventLog -LogName System -EntryType Error -After $startDate | Select-Object -Property EventId, Message, Source, InstanceId, TimeGenerated, $ReplacementStrings, UserName | ConvertTo-Html -PreContent $preContent -PostContent $postContent | Set-Content -Path $Path
Invoke-Item -Path $Path
|