ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Solved Getting PowerShell to provide me the EventLog details in an email

    IT Discussion
    powershell automation email system administration
    2
    6
    659
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DustinB3403D
      DustinB3403
      last edited by

      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?

      1 Reply Last reply Reply Quote 3
      • DustinB3403D
        DustinB3403
        last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • DustinB3403D
          DustinB3403
          last edited by

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

          Eh. . Eh. . Eh. .

          1 Reply Last reply Reply Quote 2
          • dafyreD
            dafyre
            last edited by dafyre

            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
            
            1 Reply Last reply Reply Quote 0
            • DustinB3403D
              DustinB3403
              last edited by

              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!

              dafyreD 1 Reply Last reply Reply Quote 0
              • dafyreD
                dafyre @DustinB3403
                last edited by

                @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!

                1 Reply Last reply Reply Quote 1
                • 1 / 1
                • First post
                  Last post