Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Formatting multiple outputs of Cmdlets into a single HTML email

$
0
0

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:

countname
2ESENT
2Windows Search Service
1LightScribeService
1gupdate
1Bonjour Service
1iPod Service
1SeaPort
1Intuit Update Service
EventIDMachineNameCategoyEntyTypeMessageSourceTimeGenerated
3044DEN2008The gatherer index resumed. Context: Application, SystemIndex Catalog Windows Search Service9/4/2013 8:45:54 PM
EventIDMachineNameCategoyEntyTypeMessageSourceTimeGenerated
0DEN2008The 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 Service9/4/2013 8:45:53 PM
EventIDMachineNameCategoyEntyTypeMessageSourceTimeGenerated
1003DEN2008The Windows Search Service started. Windows Search Service9/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?


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>