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

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Vandrey Trindade Vandrey Trindade 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #41531
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Hi,

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


    $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
    If ($LogVerification) {
    $emailFrom = "[email protected]"
    $emailTo = "[email protected]"
    $subject = "Events 55 found on SRV-AD"
    $body = "There were NTFS error records on event viewer of the server.`rPlease check those records.`r$LogVerification"
    $smtpServer = "192.168.0.20"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($emailFrom, $emailTo, $subject, $body)
    }
    else {
    Write-Warning "No event found!"
    }

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


    $body = Format-List -InputObject $LogVerification | Out-String

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

    #41542
    Profile photo of Wes Stahler
    Wes Stahler
    Participant

    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.

    ,Format-Table -Wrap -AutoSize | Out-String
    #41626
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Wes Stahler,

    Worked! 😀
    Thanks a lot for the fast reply!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.