Email Powershell-用于通过电子邮件发送的正文信息的格式化输出
我有一个问题,我检索了一个特定文件夹中的文件列表。很好!。我现在正试图在电子邮件正文中格式化它。 第一个到第二列的数据很好,但第三个是文件大小(我想右对齐它)一直不正常 我在网上尝试了许多不同的格式选项,但似乎都不起作用 以下是该计划的主要部分:Email Powershell-用于通过电子邮件发送的正文信息的格式化输出,email,powershell,smtp,format,Email,Powershell,Smtp,Format,我有一个问题,我检索了一个特定文件夹中的文件列表。很好!。我现在正试图在电子邮件正文中格式化它。 第一个到第二列的数据很好,但第三个是文件大小(我想右对齐它)一直不正常 我在网上尝试了许多不同的格式选项,但似乎都不起作用 以下是该计划的主要部分: #Get List of files in Rejected Folder $files = (Get-ChildItem -Path $path -Recurse| Where-Object { !$_.PSIsContainer
#Get List of files in Rejected Folder
$files = (Get-ChildItem -Path $path -Recurse| Where-Object { !$_.PSIsContainer })
#Setup eMail properties
$From = "xxxxxx@yyyyyy.zzz"
$To = "bbbbbbbbb"
$Subject = "Files in TEST Reject Folder"
$SMTPServer = "mail.nswhealth.net"
$SMTPPort = "25"
#Setup the Body of the eMail which will include header and list of files in Rejected Folder
$Body = "Here is a list of the files: <p>"
$mailBody = "Filename `t`t`t`t`t`tCreationTime `t`t`t Size`r`n"
$mailBody += "------------ `t`t`t`t`t`t-----------------`t`t`t -----`r`n"
$Count = 0 #Initialise counter
#Get list of files and append into string with 1 file per line
foreach ($file in $files) {
$kb = ($([math]::Round($file.Length / 1kb)) -as [string]).ToString().Trim();
#$kb1 = $kb + "KB"
#$varLine1 = $file.Name + "`t" + $file.CreationTime + "{0,15}" -f $kb1 + "`r`n"
#Reformat Date
$varDate = $file.CreationTime
$varDate1 = $varDate.ToString("dd/MM/yyyy hh:mm tt")
#$varLine1 = $file.Name + "`t" + $varDate1 + "{0,15}" -f $kb + "`r`n"
#$varLine1 = $file.Name + "`t" + $varDate1 + "`t" + $kb + " KB" + "`r`n"
$varLine1 = "{0,-50} {1,2} {2,-19} {3,2} {4,15} {5,3} {6,4}" -f $file.Name, "`t", $varDate1, "`t", $kb, "KB", "`r`n"
$mailBody += $varLine1;
$Count++; #increment counter
}
#When complete list has been setup in body, add a total file counter to the end
$mailBody += "`r`nTotal Number of Files: " + $Count
#Send
$smtp = new-object Net.Mail.SmtpClient($SMTPServer)
$smtp.Send($From, $To, $Subject, $mailBody)
#获取被拒绝文件夹中的文件列表
$files=(Get ChildItem-Path$Path-Recurse | Where对象{!$\uuz.PSIsContainer})
#设置电子邮件属性
$From=”xxxxxx@yyyyyy.zzz"
$To=“bbbbb”
$Subject=“测试拒绝文件夹中的文件”
$SMTPServer=“mail.nswhealth.net”
$SMTPPort=“25”
#设置电子邮件正文,其中将包括标题和被拒绝文件夹中的文件列表
$Body=“这是一个文件列表:”
$mailBody=“Filename`t`t`t`t`t`tCreationTime`t`t`t Size`r`n”
$mailBody+=“------------`t`t`t`t`t`t------------`t`t`t------------`r`n”
$Count=0#初始化计数器
#获取文件列表并附加到字符串中,每行1个文件
foreach($files中的文件){
$kb=($([math]::Round($file.Length/1kb))-as[string]).ToString().Trim();
#$kb1=$kb+“kb”
#$varLine1=$file.Name+“`t”+$file.CreationTime+“{0,15}”-f$kb1+“`r`n”
#重新格式化日期
$varDate=$file.CreationTime
$varDate1=$varDate.ToString(“dd/MM/yyyy hh:MM tt”)
#$varLine1=$file.Name+“`t”+$varDate1+“{0,15}”-f$kb+“`r`n”
#$varLine1=$file.Name+“`t”+$varDate1+“`t”+$kb+“kb”+“`r`n”
$varLine1=“{0,-50}{1,2}{2,-19}{3,2}{4,15}{5,3}{6,4}”-f$file.Name,`t',$varDate1,`t',$kb,`r`n”
$mailBody+=$varLine1;
$Count++#递增计数器
}
#在正文中设置完完整列表后,在末尾添加一个文件总数计数器
$mailBody+=“`r`n文件总数:”+$Count
#发送
$smtp=新对象Net.Mail.SmtpClient($SMTPServer)
$smtp.Send($From,$To,$Subject,$mailBody)
我附上了一张它看起来像什么的照片,不确定它是否显示正常。但这里有一个它看起来像什么的模型。正如你所看到的,前两栏是可以的,但第三栏总是有点出格(不能像我在电子邮件中看到的那样精确地模拟出来)
文件名创建时间大小
------------ ----------------- -----
000000AB0001.ref 10/11/2015 05:00 PM 86 KB0113585H0000.ref 15/05/2015 03:10 PM 152 KB
0264621J0000.ref 15/05/2015 02:50 PM 125 KB 任何帮助都将不胜感激
Andrew我认为,您需要使用
pre
标记将预先格式化的文本包含到电子邮件中:
$mailBody = -join @(
'Here is a list of the files: <p>'
'<pre>'
$files|Format-Table -AutoSize `
-Property @{Label='Filename';Expression='Name'},
@{Expression='CreationTime';Format='dd/MM/yyyy hh:mm tt'},
@{Label='Size';Expression={"$([math]::Round($_.Length / 1kb)) KB"};Alignment='right'} |
Out-String -Width ([int]::MaxValue)
'</pre>'
"Total Number of Files: $($files.Count)"
)
$mailBody=-join@(
'这是文件列表:'
''
$files |格式表-自动调整大小`
-属性@{Label='Filename';Expression='Name'},
@{Expression='CreationTime';Format='dd/MM/yyyy hh:MM tt'},
@{Label='Size';Expression={“$([math]::Round($.Length/1kb))KB};Alignment='right'}|
输出字符串-宽度([int]::MaxValue)
''
“文件总数:$($Files.Count)”
)
您的格式设置正确(在间距方面)。如果您尝试使用monospace字体查看邮件,您将看到:
因此,请将电子邮件客户端设置为使用monospace字体或使用电子邮件客户端cat能够理解的标记:HTML、BBCode等。PetSerAl给出了一个HTML电子邮件的好例子,其中正文使用Format Table cmdlet格式化,并放置在
pre
标记中。感谢您的回答。以下是电子邮件中发送的内容。以下是文件列表:Filename CreationTime Size-------------------000000AB-0700024-201511016504042045700001.ref 10/11/2015 05:00 PM 86 KB 0113585H-0844817-20150511509330916000.ref 15/05/2015 03:10 PM 152 KB0264621J-0844802-20150514483005570000.ref 15/05/2015 02:50 PM 125 KB文件总数:12详细信息中没有换行符,只是一个连续字符串。其他行有换行符。如果我使用send mailmessage而不是$smtp,会有什么不同吗?下面是一个文件列表:Filename CreationTime Size---------------000000AB-0700024-20151101650420457001.ref 10/11/2015 05:00 PM 86 KB0113585H-0844817-201505150933091600.ref 15/05/2015 03:10 PM 152KB@AndrewH当你使用我的代码时,你对格式有什么问题吗?我不能很好地理解你的评论。嗨,PetSerAl,格式不是在你的代码的每一行末尾添加换行符。当我添加$msg=newobject Net.Mail.Mailmessage($From,$To,$Subject,$mailBody.ToString())$msg.IsBodyHTML=$true时,格式变得完美。所以再次感谢你的帮助
000000AB0001.ref 10/11/2015 05:00 PM 186 KB