2

我使用下面的代碼在PowerShell中從一個純文本文件得到正確的表格格式電子郵件output.txt的電子郵件正文格式和修改

code.ps1

$smtpServer = "[email protected]" 
$smtpFrom = "[email protected]" 
$smtpTo = "[email protected]" 
$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 

但是,我怎麼能只在像下面中心對齊的第二和第三列中的值?

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 

Change2:

如何,我可以在郵件正文的末尾添加下面的注意事項,表外:

All memory values are in GB. 

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

Change3:

如何我可以給這封電子郵件給多個CC的多個收件人。我可以添加以逗號分隔的電子郵件ID:$ smtpTo =「administrator @ abc.com,dbadmin @ abc.com ...」 ..?

回答

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你是正確的:添加由,

+0

分離是多個電子郵件不會忽略..發現錯誤,並糾正..工作完美現在..感謝很多Ocaso !! ..所有三個變化取得成功..!:) – Sunny

相關問題