問題描述
試圖從事件日誌中獲取數據到電子郵件中的 html (Trying to get data from event logs into html in email)
我正在嘗試將事件日誌條目添加到 html 中,我已經成功地與其他 cmdlet 抗爭了太久。這就是我正在做的事情:
Get‑EventLog ‑logName EDEN.AppServer ‑AFTER $lastCheck.Date | Where‑Object {$_.EntryType ‑eq 'Error'}
但是一旦我將它分配給變量並堅持html 我明白了。
$html = @"
<!doctype html>
<html lang="en">
<head>
</head>
<body>
<img src = "cid:logo.jpg"/>
<div>
<P></P>
<h>Eden Application Server Information</h>
<p>Computer Name: $($EasHostInfo.Name)</p>
<p>EAS Version: $($easVersion.Version) </p>
<p>Eden Version: $($EasEdenVersion.LastVersion)</p>
<p>EAS Error Log: There was$($EasLog)</p>
</div>
</body>
</html>
"@
任何幫助將不勝感激。
參考解法
方法 1:
You are not showing us the middle steps as to how you made $EasEdenVersion
but it does not particularly matter in this case. You are trying to squeeze complex object into a single <p>
. If I had to guess you are putting in all the name
s of returned records into one paragraph.
You have objects but you need to create structured html in order to have it displayed properly. ConvertTo‑HTML
in some way should be able to help here. While it can create whole files and support CSS you already have the start of a file so lets see if we can work with that.
$html = @"
<!doctype html>
<html lang="en">
<head>
</head>
<body>
<img src = "cid:logo.jpg"/>
<div>
<P></P>
<h>Eden Application Server Information</h>
{0}
</div>
</body>
</html>
"@
$table = Get‑EventLog ‑logName Application ‑AFTER (Get‑Date).AddDays(‑6) |
Where‑Object {$_.EntryType ‑eq 'Error'} |
ConvertTo‑Html ‑Fragment | Out‑String
$html ‑f $table
Mostly to prove a concept I have edited your $html
with a placeholder {0}
for a HTML table. That table comes from the output of Get‑EventLog
which is converted to a HTML table using ‑Fragment
and saved as one whole string. That string is then inserted into the $html
using the format operator.
You will notice that I have not selected any specific properties and that not all properties were saved as readable strings. You are going to need to use Select‑Object
to get the properties you want like name
, version
and lastversion
.