Powershell很新。编写了PowerShell脚本,该脚本工作正常,但是当它发送邮件时,它不会在正文中显示新行字符。以下是配置:
$EmailFrom = "monitoring@mydomainname.no"
$EmailTo = "fatherazrael@tcs.com"
$Subject = "Disk Space Low: $server"
$Body = "Server Name: $server, <NEED NEW LINE> Drive: C, <NEED NEW LINE> Total Size: $sizeGB, <NEED NEW LINE> Space Left: $freeSpaceGB"
$SMTPServer = "scan.opinergo.fn"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("<From mail ID>", "Password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
在Body中我使用了以下代替;四处搜寻:
1)%0d%0a - 用户检查邮件时不工作
2)\ n - 用户检查邮件时不工作
3)&lt; br&gt; - 用户检查邮件时不工作
有人可以建议我还能用什么来获取新行或脚本中所需的任何修改?
答案 0 :(得分:5)
(来自我的评论)
PowerShell换行符指示符是'n(反引号-n),所以:
$Body = "Server Name: $server, <NEED NEW LINE> Drive: C
变为:
$Body = "Server Name: $server, `n Drive: C
答案 1 :(得分:2)
我不确定这些其他用户如何能够创建一个新行,但我永远无法让它工作......
相反,我最终做了以下......
-BodyAsHtml
开关<br />
(中断)html标记例如......
powershell -ExecutionPolicy ByPass -Command Send-MailMessage -BodyAsHtml -SmtpServer 'myemail.server.com' -To 'Mr To <to@server.com>' -From 'Mr From <from@server.com>' -Subject 'Reports From Daily SQL Backup Jobs' -Body 'There was an issue backing up the databases and sending them to the virtual drive.<br />Test Line 2.<br />Test Line 3.-more on line 3<br />Test Line 4.<br />EOF<br />'"
答案 2 :(得分:1)
尽管我使用的是4.0版,但只需按照您希望其显示的方式将其放入脚本中就可以了。不知道您使用的是哪个版本,或者是否有所不同。
$Body = "Server Name: $server
Drive: C
Total Size: $sizeGB
Space Left: $freeSpaceGB"
答案 3 :(得分:0)
PowerShell换行符指示符是n,只能用于双引号。
$Body = "Server Name: $server, <NEED NEW LINE> Drive: C"
变为:
$Body = "Server Name: $server, `n Drive: C"
答案 4 :(得分:0)
从技术上讲,它必须是
$body = "first line" + "`n" + "second line"
否则,第一行创建为结尾,第二行创建为开头。