3

我在 Powershell 中使用以下代码从纯文本文件output.txt中以正确的表格格式获取电子邮件:

代码.ps1

$smtpServer = "sunny@domain.com"
$smtpFrom = "process@abc.com"
$smtpTo = "sunny@abc.com"
$messageSubject = "Data"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$body = Get-Content C:\output.txt | Out-String
$body = $body -replace '^(\S+)\s+(\S+)\s+(\S+)', '<tr><th style = "border: 1px solid black; background: #dddddd; padding: 5px;">$1</th><th style = "border: 1px solid black;   background: #dddddd; padding: 5px;">$2</th><th style = "border: 1px solid black; background: #dddddd; padding: 5px;">$3</th></tr>'
$body = $body -replace '\n(\S+)\s+(\S+)\s+(\S+)', '<tr><td style = "border: 1px solid black; padding: 5px;">$1</td><td style = "border: 1px solid black; padding: 5px;">$2</td><td style = "border: 1px solid black; padding: 5px;">$3</td></tr>'
$body = '<body><table style = "border: 1px solid black; border-collapse: collapse;">' + $body + '</table></body>'
$message.Body = $body

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

我想对接收电子邮件结构进行以下更改:

变化1:

电子邮件在表格中采用以下格式:

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

但是,我怎样才能将第 2 列和第 3 列的值仅在中心对齐,如下所示?

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

变化2:

我如何在电子邮件正文表格之外的末尾添加以下注释:

All memory values are in GB.

Note: This is an automatically generated email, please don't reply to it.

变化3:

我如何将此电子邮件发送给具有多个 CC 的多个收件人。我可以添加以逗号分隔的电子邮件 ID,例如$smtpTo = "administrator@abc.com,dbadmin@abc.com..." ..?

4

1 回答 1

2

Change1:更改表格行定义

$body = $body -replace '\n(\S+)\s+(\S+)\s+(\S+)', '<tr><td style = "border: 1px solid black; padding: 5px;">$1</td><td style = "border: 1px solid black; padding: 5px;">$2</td><td style = "border: 1px solid black; padding: 5px;">$3</td></tr>'

$body = $body -replace '\n(\S+)\s+(\S+)\s+(\S+)', '<tr><td style = "border: 1px solid black; padding: 5px;">$1</td><td style = "border: 1px solid black; padding: 5px;text-align:center;">$2</td><td style = "border: 1px solid black; padding: 5px;text-align:center;">$3</td></tr>'

说明:添加text-align:center到表格单元格的样式定义中。

Change2:只需在邮件末尾添加文本:

$body = '<body><table style = "border: 1px solid black; border-collapse: collapse;">' + $body + '</table><br/>All memory values are in GB.<br/><br/>Note: This is an automatically generated email, please don't reply to it.<br/></body>'

Change3:根据http://msdn.microsoft.com/en-us/library/vstudio/h1s04he7(v=vs.100).aspx你是对的:添加多个电子邮件地址,

于 2013-10-31T09:19:06.183 回答