четверг, 3 июля 2014 г.

Некорректный разбор адреса отправителя в заголовке входящего почтового сообщения из non-microsoft почтового сервера в SCSM 2012 R2

Недавно столкнулся с интересной особенностью Service Manager  2012 R2 при настройке обработки входящей почты из Lotus Notes для создания инцидентов.

Обратите внимание на красную скобку - такое происходило при получении писем от более чем 10 различных адресатов, использующих SCSM 2012 R2. 

Письмо появлялось в папке C:\inetpub\mailroot\Drop\ и вроде нормально обрабатывалось, однако, как видно из рисунка ниже,  система почему-то использовала произвольный почтовый адрес, заменяя тот, который был указан в поле From. 


Причина кроется в  поле From - почтовый адрес не был взят в < >. 



Чтобы победить этот недуг я написал скрипт (см. ниже), который поместил в автозагрузку при старте системы  (Local GPO, через gpedit.msc)  без выполнения логона какого-либо пользователя.

Собственно сам скрипт - немного кривоватый, но пользоваться можно:

#Version 1.0
#--------------------------------------------------------------------------

#Description
#-------------------------------------------------------------------------
#This script replaces incorrect syntax in  string of *.eml files: "From: email@address.domain" to "From: <email@address.domain>" 
#General problem described here: http://social.technet.microsoft.com/Forums/systemcenter/en-US/b3fd6aec-0c7a-4121-af49-90c473c8293b/affected-user-in-incident-created-by-mail-is-wrong?forum=systemcenterservicemanager
#That makes SCSM 2012 R2 SMTP E-mail ServiceManager.InboundEmail.Configuration.xml management pack work correctly during workflow of parsing incoming e-mails
#To use this script you need to place it as a task into the Scheduler of the Windows Server which you're using as a Root server of the SCSM MGMT Group.
#It's true only for SMTP and not for Exchange Connector

#Setting up infinity cycle
while(1)
{
#Forming an Array of the *.eml files;
$MyArray =  Get-Item C:\inetpub\mailroot\Drop\* | Where-Object {$_.Name -like "*.eml"}

#Write-Host "The array contains: " $MyArray  

Foreach ($ArrayMember in $MyArray)
{
 #Write-Host "For the member " $ArrayMember "of the array"
         #Searching for a text pattern in each letter to identify a need of change
$A = Select-String -path $ArrayMember -pattern "From: <"
 
          #Write-Host $A

          #Checking if  < > are already used;
  IF ($A -eq $null)
{
                 #If <> are  not used - do changes;
#Getting content of the E-mail file that is a member of a $MyArray;
$OriginalString = Get-Content $ArrayMember
 
#Changing strings;
$OriginalString = $OriginalString -replace "From: ","From: <"
$OriginalString = $OriginalString  -replace '@target.domain', '@target.domain>'
 
#Writing to same file correct information
$OriginalString | Out-File $ArrayMember
         
                 #Write-Host "...E-Mail "  $ArrayMember " corrected"
}
  ELSE
{
#Write-Host "...string not found"
}
}
start-sleep -seconds 1
}