試圖從事件日誌中獲取數據到電子郵件中的 html (Trying to get data from event logs into html in email)


問題描述

試圖從事件日誌中獲取數據到電子郵件中的 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'}

在控制台中,輸出看起來像我想要的 consoleOut

但是一旦我將它分配給變量並堅持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>
"@

In 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 names 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.

Sample Output

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.

(by BFSMatt)

參考文件

  1. Trying to get data from event logs into html in email (CC BY‑SA 2.5/3.0/4.0)

#powershell






相關問題

玩弄 C# 加密 (Playing around with C# encryption)

查找具有特定 startname 的特定服務 (Finding specific services with specific startname)

試圖從事件日誌中獲取數據到電子郵件中的 html (Trying to get data from event logs into html in email)

如何通過powershell獲取請求的authtoken (How to obtain authtoken for request via powershell)

如何在錯誤時繼續使用 &$var 調用可執行文件 (How to continue on error a call to an executable using &$var)

在同一範圍內從 c# 調用多個 powershell 命令 (Invoke multiple powershell commands from c# on the same scope)

如何在 Powershell 二進制模塊(.Net Standard 和 Nuget)中處理公共和私有依賴項和打包 (How to handle public and private dependencies and packaging in Powershell binary module (.Net Standard & Nuget))

如何使用參數(描述空白)獲取廣告中的所有 OU - 沒有描述? (How to get all OU in Ad with parameter (description blank ) - without description?)

遠程服務器返回錯誤:(400) 錯誤請求。在 C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1 (The remote server returned an error: (400) Bad Request. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1)

Powershell 檢查文件類型 (Powershell check file type)

服務器待重啟 (Server Pending Reboot)

PowerShell 的 Invoke-WebRequest 在 Docker 容器中不起作用 (PowerShell's Invoke-WebRequest not working within a Docker Container)







留言討論