发送PowerShell电子邮件时格式化txt文件

时间:2013-12-17 15:56:08

标签: email formatting powershell-v2.0

我必须将一个powershell脚本的输出发送到outlook \ external email,它到达目的地时看起来非常糟糕。 任何想法如何格式化它看起来像原始?

这就是我原来的.txt的样子:

这里的一些文字

Directory: C:\Users\xxx\Desktop

-a---         12/6/2013   2:31 PM      30823 diagram.docx
-a---        12/16/2013   2:17 PM       2002 Microsoft Outlook 2010.lnk
-a---        12/10/2013   2:25 PM       1576 new vlans.txt
-a---        12/12/2013   9:39 AM         59 pass.txt
-a---        12/11/2013   1:57 PM      14764 vlans for Q9.xlsx
-a---        12/10/2013   2:19 PM     182784 _Release Request Form V12.doc
Total size GB
--------------
0.450272973626852  

电子邮件:

enter image description here

这是我用过的代码:

 'New-Item c:\users\xxx\desktop\pwr -type directory
echo "Good morning gentlemen, below are this week's  bakups report!" > c:\users\xxx\desktop\pwr\bkp.txt

'(Get-ChildItem -Path C:\Users\xxx\Desktop\*.*  | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-10) }) >> c:\users\xxx\desktop\pwr\bkp.txt

'(Get-ChildItem -Path C:\Users\xxx\Desktop\*.*  | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-10) }).Count > c:\users\xxx\desktop\pwr\tmp.txt

'Get-ChildItem c:\users\xxx\desktop -recurse | Measure-Object -property length -sum|ft @{Name="Total size GB "; Expression={$_.Sum / 1GB};align="left"} >> c:\users\xxx\desktop\pwr\bkp.txt 

'gwmi win32_volume -Filter 'drivetype = 3' | select driveletter, label, @{LABEL='GB freespace';EXPRESSION={$_.capacity / 1gb }} >> c:\users\xxx\desktop\pwr\tmp.txt
Get-Date -Format "dd/MM/yyyy" >> c:\users\xxx\desktop\pwr\tmp.txt


$ol = New-Object -comObject Outlook.Application  
$mail = $ol.CreateItem(0)  
$Mail.Recipients.Add("email@domain.com")  
$mail.Subject = "Weekly PME Backups $((get-date -format "dd/MM/yyyy"))" 
$Mail.Body = (Get-content "C:\Users\xxx\Desktop\pwr\bkp.txt")  
$Mail.Send()

谢谢! Ciprian

1 个答案:

答案 0 :(得分:1)

终于想通了:)测试并使用outlook

$ol = New-Object -comObject Outlook.Application  
$mail = $ol.CreateItem(0)  
$Mail.Recipients.Add("email@domain.com")  
$mail.Subject = "Weekly  Backups $((get-date -format "dd/MM/yyyy"))" 
$Mail.Body = (Get-content "C:\Users\xxx\Desktop\pwr\bkp.txt")    
$Mail.body = $body -join "`n"
$Mail.Send()