我正在使用此 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)