Getting PowerShell to provide me the EventLog details in an email



  • OK so I have a working script (mostly) it is below.

    # We first need to know which command to use
    Get-Command '*mail*'
    
    # We use the splatting technique to provide the parameters
    $Params = @{
    	SmtpServer = 'smtp.office365.com'
    	Port       = '587'
    	From       = '[email protected]'
    	To         = '[email protected]'
    	Subject    = 'Low Disk Space'
    	Body       = 'These Server Disks are Low on memory.'
    }
    
    # Get-Help explains what this CmdLet does
    #Get-Help Send-MailMessage
    
    # Get-Help can also give you examples on how to use the CmdLet
    #Get-Help Send-MailMessage -Examples
    
    # Retrieve only events of the last 24 hours and select the first one
    $Today = Get-Date
    $Past = $Today.AddDays(-1) 
    $Event = Get-EventLog -LogName System -After $Past | Where-Object {$_.EventID -eq 2013} | Select-Object -First 200
    
    # Add the event to the mail body
    $Params.Body += ' ' + $Event.Message
    
    # Send the mail
    Send-MailMessage @Params -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "[email protected]",(Get-Content -Path C:\Windows\system32\[email protected] | ConvertTo-SecureString)) -UseSsl
    

    The issue is that the event logs that get emailed to me for EventID 2013 are blank, nothing there in the body the email I receive.

    I know this works as I can dump another EventID into that field and I get the details.

    What am I missing here?



  • I've tried to | select message and there is no change in the results, which also shouldn't be required as I want the entire Event detail, the drive letter and the error message.

    With another event I tested with I got this for the message body These Server Disks are Low on memory. The system uptime is 1651546 seconds. The system uptime is 1651544 seconds.



  • Doh wait I might be an idiot. . . there are no EventID within the past day. . .

    Eh. . Eh. . Eh. .



  • Here is the way I have my Send-MailMessage formatted in a script I run by hand.

    $creds=get-credential
    $to="[email protected]"
    $from="[email protected]"
    $subject="Dumb Computer Issues"
    
    $Past=(Get-date).addDays(-1)
    $Event = Get-EventLog -LogName System -After $Past | Where-Object {$_.EventID -eq 2013} | Select-Object -First 200
    
    $body=$Event
    
    Send-MailMessage -From $fromEmail -To $toEmail -Subject $subject -Body $body  -smtpServer "smtp.office365.com" -credential $creds -UseSsl -Port 587
    


  • Yup. . . I'm an idiot lol. I set it to only look back 1 day and email me those results within the past day.

    This obviously would show blank results, since there are no 2013 events within the past day!



  • @dustinb3403 said in Getting PowerShell to provide me the EventLog details in an email:

    Yup. . . I'm an idiot lol. I set it to only look back 1 day and email me those results within the past day.

    This obviously would show blank results, since there are no 2013 events within the past day!

    Ha ha ha. Whoops!