0

I am using this power-shell script to send emails. My problem is with the $body variable. I don't want to attach the log file in the email, I want the contents of the log file to be in the body of the email.

Write-Host "Setting Variables" -ForegroundColor Green
    $user    = "username"
    $pass    = Get-Content "$PSScriptRoot\pass.txt" | ConvertTo-SecureString
    $cred    = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $user,$pass
    $from    = "[email protected]"
    $to      = "[email protected]"
    $subject = "Email Subject"
    $body    = Get-Content C:\backup\log.txt
    $smtp    = "smtp.gmail.com"

    Write-Host "Sending Email" -ForegroundColor Green
    Send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $smtp -Port 587 -UseSsl -Credential $cred

When I run the script I get a message saying:

Send-MailMessage : Cannot convert 'System.Object[]' to the type 
'System.String' required by parameter 'Body'. Specified method is not 
supported.

How can I read the contents of the log file and put it in the body of the email?

1
  • What do you get in powershell when issuing Get-Content C:\backup\log.txt? Maybe an encoding problem? Did you try casting to [string]?
    – LotPings
    May 17, 2017 at 18:51

1 Answer 1

2

this solved it

-Body ($body | Out-String)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .