Powershell通过EWS发送电子邮件消息正文格式

时间:2015-07-30 13:51:55

标签: string email powershell outlook exchangewebservices

我创建了一个脚本,该脚本将运行我们从Cyber​​Ark(密码管理)获取的CSV文件的报告,以查看密码是否超过特定天数,并向管理帐户/资产的人发送电子邮件。< / p>

但是,当我发送电子邮件时,即使我认为我正确格式化了邮件正文(在POSH中输出正常),邮件正文也会格式化。所以,也许我做错了,或者是在EWS中读取文本的方式。我将在下面举例说明我的尝试。所有这些变量都通过数组读取,并循环遍历每条记录,直到完成所有记录。

Here-字符串代码(尝试使用和不使用[string]和相同的结果):

[string]$body = @"
Hello,

$notice

Target System: $targetSysEmail
Target Account: $targetAccEmail
Target: $targetEmail
Safe: $safeEmail
Last Password Change: $lastChangeEmail
Current Password Age in days: $currentAge
Days Overdue: $daysOverdueEmail
"@

电子邮件示例:

Hello,

This account requires your attention right now:

Target System: some_oracle_server
Target Account: SOME_USER
Target: ROOT for Asset
Safe: MONEY
Last Password Change: 04/21/2015 00:00:00 Current Password Age in days: 97 Days Overdue: 7

正如您所看到的,没有保留换行符。

我尝试的另一个示例代码(尝试使用&amp; w / o [String]):

[string]$body = "Hello, `n`n" + $notice + "`n`nTarget System: " + $targetSysEmail + "`nTarget Account: " + $targetAccEmail +
"`nTarget: " + $targetEmail + "`nSafe: " + $safeEmail + "`nLast Password Change Date: " + $lastChangeEmail + 
"`nCurrent Password Age: " + $currentAge + " days old" + "`nDue Date: " + $dueDateEmail + "`nDays Until Due: " + $daysUntilEmail

电子邮件示例:

Hello, 

This account requires your attention in the upcoming weeks:

Target System: some_cool_system
Target Account: account
Target: COOL_root
Safe: cashcash
Last Password Change Date: 05/06/2015 00:00:00 Current Password Age: 82 days old Due Date: 08/04/2015 00:00:00 Days Until Due: 8

我想要的只是每一件事都在新的一条线上,这真的无法发挥作用。我尝试将正文输出到一个文本文件,看起来格式正确,然后再读回来尝试,但无济于事。

查看是否有错误的完整代码:

#region Email Portion
if($email -eq 'YES')
{
#grab all the information from the new worksheet to then use for emailing the violators.
Write-Host 'Putting all the information into arrays'
$lastRow2 = $workSheet2.UsedRange.Rows.Count
$lastChange = @()
$alias = @()
$safe = @()
$target = @()
$targetSystem = @()
$targetAccount = @()
$currentPWAge = @()
$daysOverdue = @()
$daysUntilDue = @()
$dueDate = @()
$level = @()
for ($loop = 2; $loop -le $lastRow2; $loop++)
{
$lastChange += $workSheet2.Range("A$loop").Value()
$alias += $workSheet2.Range("B$loop").Value()
$safe += $workSheet2.Range("D$loop").Value()
$target += $workSheet2.Range("E$loop").Value()
$targetSystem += $workSheet2.Range("G$loop").Value()
$targetAccount += $workSheet2.Range("H$loop").Value()
$currentPWAge += $workSheet2.Range("N$loop").Value()
$daysOverdue += $workSheet2.Range("O$loop").Value()
$daysUntilDue += $workSheet2.Range("P$loop").Value()
$dueDate += $workSheet2.Range("Q$loop").Value()
$level += $workSheet2.Range("R$loop").Value()
}

#use the previously collected info to formulate emails
#default email is my address, so change it to $email = Get-AdMailAddress (alias[$loop]) instead of $email = Get-AdMailAddress ("MY_ID") if running the real report
Write-Host 'Using the arrays to send out emails'
$n = '`n'
for ($g = 0; $g -le ($lastRow2 - 2); $g++)
{
$lastChangeEmail = $lastChange[$g]
$emailAd = Get-AdMailAddress ("MY_ID")
$safeEmail = $safe[$g]
$targetEmail = $target[$g]
$targetSysEmail = $targetSystem[$g]
$targetAccEmail = $targetAccount[$g]
$currentAge = [int]$currentPWAge[$g]
$daysOverdueEmail = $daysOverdue[$g]
$daysUntilEmail = $daysUntilDue[$g]
$dueDateEmail = $dueDate[$g]
$levelEmail = $level[$g]

if($levelEmail -eq "Red")
{
$subject = "URGENT: One Account Managed by You is in Violation of ___ 90 Day Password Life Policy"
$importance = 'High'
$notice = 'This account requires your attention right now:'
} 
elseif ($levelEmail -eq "Orange")
{
$subject = "URGENT: Password Change Required ASAP for this Account"
$importance = 'High'
$notice = 'This account requires your attention in the near future:'
} 
elseif ($levelEmail -eq "Yellow")
{
$subject = "Reminder: Password Change for this Account is Required Within a Week"
$importance = 'Normal'
$notice = 'This account requires your attention soon:'
} 
elseif ($levelEmail -eq "Green")
{
$subject = "Notification: Password Change for this Account is Required In the Next Two Weeks"
$importance = 'Low'
$notice = 'This account requires your attention in the upcoming weeks:'
}

if($importance -eq 'High')
{
[string]$body = @"
Hello,

$notice

Target System: $targetSysEmail
Target Account: $targetAccEmail
Target: $targetEmail
Safe: $safeEmail
Last Password Change: $lastChangeEmail
Current Password Age in days: $currentAge
Days Overdue: $daysOverdueEmail
"@
} 
elseif($importance -ne 'High')
{
[string]$body = "Hello, `n`n" + $notice + "`n`nTarget System: " + $targetSysEmail + "`nTarget Account: " + $targetAccEmail +
"`nTarget: " + $targetEmail + "`nSafe: " + $safeEmail + "`nLast Password Change Date: " + $lastChangeEmail + 
"`nCurrent Password Age: " + $currentAge + " days old" + "`nDue Date: " + $dueDateEmail + "`nDays Until Due: " + $daysUntilEmail
}

if ($importance -ne 'Normal')
{
Send-MailMessageViaEWS -To $emailAd -Subject $subject -body $body -Importance $importance
Sleep 1
} 
elseif ($importance -eq 'Normal')
{
Send-MailMessageViaEWS -To $emailAd -Subject $subject -body $body
Sleep 1
}
}
} #end first if
#endregion Email Portion

我花了太多时间试图搞清楚,我很难过。任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

EWS托管API中的默认正文类型是HTML,当您发送邮件时,您还没有包含Send-MailMessageViaEWS函数的代码,但我会说如果您没有明确告诉EWS您使用Text正文那是你的问题。 (如果要发送HTML正文,请使用

<BR>

而不是

`r`n 

托管API不会为您转换这些内容)。但我建议你只使用Text body指示符,例如

$EmailMessage = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service  
$EmailMessage.Subject = "Message Subject"  
#Add Recipients    
$EmailMessage.ToRecipients.Add("user@domain.com")  
$EmailMessage.Body = New-Object Microsoft.Exchange.WebServices.Data.MessageBody  
$EmailMessage.Body.BodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text  
$EmailMessage.Body.Text = "Body" 
$EmailMessage.SendAndSaveCopy()  

干杯 格伦