The object of the code is to get a summary list of events from the Windows Event Log and then produce a detailed list of each entry from the Windows event logs. Later I will enhance the code to only get the detailed records of very specific entries. My problem is in formatting the output of the detailed records. Here is the Code:
$EventLog = "Application"
$NewestEvents = 10
$Events = get-eventlog -LogName $Eventlog -newest $newestEvents
#SMTP server name
$smtpServer = "Your.smtp-server"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = "Youraddress@yourdomain"
$msg.ReplyTo = "Youraddress@yourdomain"
$msg.To.Add("Youraddress@yourdomain")
$msg.subject = "From Powershell"
$body2 = ($events | group-object -property source -noelement | sort-object -property count –descending | ConvertTo-HTML -property count,name)
$body2 += foreach ($Event in $events)
{$event | select-object -property EventID, MachineName,Categoy, EntyType, Message, Source, TimeGenerated |
ConvertTo-HTML -property EventID, MachineName,Categoy, EntyType, Message, Source, TimeGenerated }
$msg.body = $body2
$msg.IsBodyHTML = $true
#Sending email
$smtp.Send($msg)
The output looks like this:
| count | name |
|---|---|
| 2 | ESENT |
| 2 | Windows Search Service |
| 1 | LightScribeService |
| 1 | gupdate |
| 1 | Bonjour Service |
| 1 | iPod Service |
| 1 | SeaPort |
| 1 | Intuit Update Service |
| EventID | MachineName | Categoy | EntyType | Message | Source | TimeGenerated |
|---|---|---|---|---|---|---|
| 3044 | DEN2008 | The gatherer index resumed. Context: Application, SystemIndex Catalog | Windows Search Service | 9/4/2013 8:45:54 PM |
| EventID | MachineName | Categoy | EntyType | Message | Source | TimeGenerated |
|---|---|---|---|---|---|---|
| 0 | DEN2008 | The description for Event ID '0' in Source 'iPod Service' cannot be found. The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them. The following information is part of the event:'Service started/resumed' | iPod Service | 9/4/2013 8:45:53 PM |
| EventID | MachineName | Categoy | EntyType | Message | Source | TimeGenerated |
|---|---|---|---|---|---|---|
| 1003 | DEN2008 | The Windows Search Service started. | Windows Search Service | 9/4/2013 8:45:50 PM |
The problem is each of the detailed rows ends up as a seperate table, with each row having the "headers". What I would like instead is one set of headers for all of the detailed rows (with the values falling under their correct headers for all rows).
Is this possible?