Export Application Eventlogs for multiple sources and send email

Lingareddy Chandrakanth Reddy 1 Reputation point
2021-12-09T02:45:39.763+00:00

156048-output.pngHi All,

I have written a script to export specific Eventvwr logs based on Source and the script is working as expected, but I am trying to cut short the message from the Eventvwr, I don't want to print an entire message on the mail.

I am attaching the output of my code below:

#  
#  This script exports consolidated and filtered event logs to CSV  
#  
  
#Application error and critical eventlogs for specific EventID:  
#Email Details  
$FromAddress = 'abc.com'  
$ToAddress = '123.com'  
$SmtpServer = 'xyz.com'  
  
Set-Variable -Name EventAgeDays -Value 4    #we will take events for the latest 7 days  
Set-Variable -Name CompArr -Value @("abcedf.com")   # replace it with your server names  
Set-Variable -Name LogNames -Value @("Application")  # Checking app and system logs  
Set-Variable -Name EventTypes -Value @("Information")  # Loading only Errors and Warnings  
Set-Variable -Name ExportFolder -Value "C:\xxx\"  
Set-Variable -Name Source -Value @("123", "111") # Add required source names from Application Eventlogs  
  
$el_c = @()   #consolidated error log  
$now=get-date  
$startdate=$now.adddays(-$EventAgeDays)  
$ExportFile=$ExportFolder + "Out" + $now.ToString("yyyy-MM-dd---hh-mm-ss") + ".csv"  # we cannot use standard delimiteds like ":"  
  
foreach($comp in $CompArr)  
{  
  foreach($log in $LogNames)  
  {  
    foreach ($src in $Source)  
    {  
    Write-Host Processing $comp\$log  
    $el = get-eventlog -ComputerName $comp -log $log -After $startdate -EntryType $EventTypes -Source $src -Newest 1 -InstanceId 30000   
    $el_c += $el  #consolidating  
  #}  
#}  
$el_sorted = $el_c | Sort-Object TimeGenerated    #sort by time  
Write-Host Exporting to $ExportFile  
}  
}   
}   
$out = $el_sorted|Select MachineName, EntryType, TimeGenerated, Source, EventID, Message #| Export-CSV $ExportFile -NoTypeInfo  #EXPORT  
Write-Host Done!  
  
$msgBody = $out  
Send-MailMessage -from $FromAddress -to $ToAddress -SmtpServer $SmtpServer -Subject "TL Stage Ingester Status from $CompArr $startdate" -Body ( $msgBody | Out-String)   

From the event log output, we will have the entire message with all the details, but I want to cut short only the above part in the image from the entire output and send it via mail

Attached the event log output which I need

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,165 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,382 questions
0 comments No comments
{count} votes