我有一个文本文件output.txt,其中包含以下内容:
OPERATINGSYSTEM PROJECTSERVER1 PROJECTSERVER2
Windows 1.36 4.42
Linux12 2.78 5.76
MacOS 3.45 6.39
Ubuntu 4.12 0.00
Android 0.00 3.46
FreePhysicalMemory 30.12 31.65
TotalVisibleMemorySize 48.00 48.00
CPULoadPercentage 2 4
我想在电子邮件中将output.txt的内容作为正文发送到Windows server 2008 R2中的格式化 HTML 表格中。我正在尝试使用下面的代码,但它对我不起作用..我在哪里弄错了?
$smtpServer = "sunny@abc.com"
$smtpFrom = "process@abc.com"
$smtpTo = "administrator@abc.com"
$messageSubject = "Servers Memory"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = Get-Content C:\Logs\output.txt
$style = "< style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "< /style>"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
编辑1
当我修改我的代码如下:
$smtpServer = "sunny@abc.com"
$smtpFrom = "process@abc.com"
$smtpTo = "administrator@abc.com"
$messageSubject = "Servers Memory"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
$message.Body = "<head><pre>$style</pre></head>"
$message.Body += Get-Content C:\Logs\output.txt
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
我收到这样的邮件:
OPERATING SYSTEM SERVER1 SERVER2 Windows 1.36 4.42 Linux 2.78 5.76 MacOS 3.45 6.39 Android 0.00 3.46 Ubuntu 4.12 0.00 FreePhysicalMemory 30.12 31.65 TotalVisibleMemorySize 48.00 48.00
注意:我使用的是 Powershell v1.0