电子邮件正文中的PowerShell电子邮件日志文件

我正在使用这个Power-Shell脚本来发送电子邮件。 我的问题是与$ bodyvariables。 我不想在电子邮件中附加日志文件,我希望日志文件的内容位于电子邮件的正文中。

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 

当我运行脚本时,我收到一条消息:

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

我怎样才能读取日志文件的内容,并把它放在电子邮件的正文中?

这解决了它

 -Body ($body | Out-String)