下面是我用来检查不同文件夹的代码的简化版本,检查最新的日志文件,然后报告最近的3个时间戳(用[
表示)。
$LogLocations = "C:\Powershell\Test\Scenario1",
"C:\Powershell\Test\Scenario2"
$style = @"
<style>BODY{font-family: Arial; font-size: 10pt;}
TABLE{border: 1px solid black; border-collapse: collapse;}
TH{border: 1px solid black; background: #dddddd; padding: 5px; }
TD{border: 1px solid black; padding: 5px; }
</style>
"@
$body = ""
foreach ($logpath in $LogLocations) {
$log = Get-ChildItem -Path $logpath |
sort LastWriteTime |
select -ExpandProperty FullName -Last 1
$text = Get-Content -Path $log |
Select-String -SimpleMatch "[" |
select Line -Last 3
$body += ($text |
select Line |
ConvertTo-Html -Body "<p>Timestamps of 3 most recent files processed $log</p>" -Head $style |
Out-String)
}
$emailbody = ConvertTo-Html -PostContent $body | Out-String
$subject = "Processing Logs Error Checking at " + (Get-Date -Format g)
Send-MailMessage -From "xxxxxxxxx" -To "xxxxxxxxx" -Subject $subject -Body $emailbody -BodyAsHtml -SmtpServer "xxxxxxxxx"
我得到的结果是:
Timestamps of 3 most recent files processed C:\Powershell\Test\Scenario1\20160520163102.txt * [05/21/2016 00:37:52] [05/21/2016 00:37:52] [05/21/2016 00:37:52] Timestamps of 3 most recent files processed C:\Powershell\Test\Scenario2\20160524142045.txt * [05/24/2016 14:38:48] [05/24/2016 14:38:51] [05/24/2016 14:38:51]
哪个好。我得到了我想要的信息,只是格式不是最好的。我试图找到一种方法将结果放在同一个表中,但是无法使代码生效,我会得到错误,没有结果或者格式化结果非常糟糕。
有什么想法吗?我正在将当前foreach
语句嵌套在表列的另一个foreach
语句中。
答案 0 :(得分:1)
如果您不想要多个表格,请不要创建多个表格。
$emailbody = $LogLocations | ForEach-Object {
$dir = $_
Get-ChildItem $dir |
Sort-Object LastWriteTime |
Select-Object -Last 1 |
Get-Content |
Where-Object { $_ -like '*[*' } |
Select-Object -Last 3 @{n='File';e={$dir.FullName}},
@{n='Timestamp';e={$_}}
} | ConvertTo-Html -Head $style