How to send mail with text and variables values on the body?

Welcome Forums General PowerShell Q&A How to send mail with text and variables values on the body?

Viewing 2 reply threads
  • Author
    Posts
    • #41531
      Participant
      Topics: 51
      Replies: 122
      Points: 78
      Rank: Member

      Hi,

      I’m trying to send and e-mail containing some variables values and text on it… That’s what I was trying:

      <br />$LogVerification = Get-EventLog -LogName System | Where {($_.EventID -eq 55) -and ($_.Message -like "*corrupt*") -and ($_.TimeGenerated -gt (Get-Date).AddDays(-1))} | select TimeGenerated,EntryType,Message | ft -Wrap -AutoSize<br />If ($LogVerification) {<br />$emailFrom = "[email protected]"<br />$emailTo = "[email protected]"<br />$subject = "Events 55 found on SRV-AD"<br />$body = "There were NTFS error records on event viewer of the server.&#8175;rPlease check those records.&#8175;r$LogVerification"<br />$smtpServer = "192.168.0.20"<br />$smtp = new-object Net.Mail.SmtpClient($smtpServer)<br />$smtp.Send($emailFrom, $emailTo, $subject, $body)<br />}<br />else {<br />Write-Warning "No event found!"<br />}<br />

      But it had a lot of “microsoft.powershell.commands.internal.format.formatstartdata” type of information…
      Then I tried using this:

      <br />$body = Format-List -InputObject $LogVerification | Out-String<br />

      It shows the correct value, but I can’t add text to send a message with it?

    • #41542
      Participant
      Topics: 2
      Replies: 50
      Points: 393
      Team MemberHelping Hand
      Rank: Contributor

      The issue is with the the $LogVerification variable. As shown in your code, it is an array. Convert it to a string and you will be good to go.

    • #41626
      Participant
      Topics: 51
      Replies: 122
      Points: 78
      Rank: Member

      Wes Stahler,

      Worked! 😀
      Thanks a lot for the fast reply!

Viewing 2 reply threads
  • The topic ‘How to send mail with text and variables values on the body?’ is closed to new replies.