![电子邮件正文中的 powershell 电子邮件日志文件](https://linux22.com/image/704408/%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6%E6%AD%A3%E6%96%87%E4%B8%AD%E7%9A%84%20powershell%20%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6.png)
我正在使用此 power-shell 脚本发送电子邮件。我的问题在于 $body 变量。我不想在电子邮件中附加日志文件,我希望日志文件的内容位于电子邮件正文中。
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.
我如何读取日志文件的内容并将其放入电子邮件正文中?
答案1
这解决了它
-Body ($body | Out-String)