Jak*_*son 1 powershell system.net.mail
我正在尝试使用Powershell输出包含一些前/后内容的表格,然后通过电子邮件发送,但前/后内容在电子邮件中显示为"System.String []".其余的内容似乎很好,如果我将HTML字符串输出到控制台,一切看起来都很好.
function Send-SMTPmail($to, $from, $subject, $smtpserver, $body) {
$mailer = new-object Net.Mail.SMTPclient($smtpserver)
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$msg.IsBodyHTML = $true
$mailer.send($msg)
}
$Content = get-process | Select ProcessName,Id
$headerString = "<table><caption> Foo. </caption>"
$footerString = "</table>"
$MyReport = $Content | ConvertTo-Html -fragment -precontent $headerString -postcontent $footerString
send-SMTPmail "my Email" "from email" "My Report Title" "My SMTP SERVER" $MyReport
Run Code Online (Sandbox Code Playgroud)
在我的电子邮件中显示为:
System.String[]
ProcessName Id
... ...
System.String[]
Run Code Online (Sandbox Code Playgroud)
执行out-file然后调用invlable-item与发送电子邮件的结果相同...
ConvertTo-Html返回一个对象列表 - 一些是字符串,一些是字符串数组,例如:
407# $headerString = "<table><caption> Foo. </caption>"
408# $footerString = "</table>"
409# $content = Get-Date | select Day, Month, Year
410# $MyReport = $Content | ConvertTo-Html -Fragment -PreContent $headerString `
-PostContent $footerString
411# $MyReport | Foreach {$_.GetType().Name}
String[]
String
String
String
String
String
String
String
String
String
String[]
Run Code Online (Sandbox Code Playgroud)
所以$ MyReport包含一个字符串和字符串数组的数组.当您将此数组传递给期望类型字符串的MailMessage构造函数时,PowerShell会尝试将其强制转换为字符串.结果是:
412# "$MyReport"
System.String[] <table> <colgroup> <col/> <col/> <col/> </colgroup> <tr><th>Day
</th><th>Month</th><th>Year</th></tr> <tr><td>9</td><td>2</td><td>2011
</td></tr> </table> System.String[]
Run Code Online (Sandbox Code Playgroud)
最简单的办法是运行的输出ConverTo-Html通过Out-String,这将导致$ MyReport是一个字符串:
413# $MyReport = $Content | ConvertTo-Html -Fragment -PreContent $headerString `
-PostContent $footerString |
Out-String
414# $MyReport | Foreach {$_.GetType().Name}
String
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8894 次 |
| 最近记录: |