A question came up today in the Orchestrator forums about processing emails with attachments.
Has anyone develop a run book that searches for emails with a specific criteria (Email is from USER@DOMAIN.COM and HasAttachment = True). Read the XML attachment as data to pass to the runbook?
My Suggestion is to just use PowerShell to accomplish this (it will set you up better for the conversion later on to SMA) and run it in a .NET script object. The Exchange PowerShell module is available at http://scorch.codeplex.com/releases/view/111490
And the script turns out like this!
Function Generate-RandomFolder
{
$random = [System.IO.Path]::GetRandomFileName()
$folderName = "c:\temp\$random"
while(Test-Path $folderName)
{
$random = [System.IO.Path]::GetRandomFileName()
$folderName = "c:\temp\$random"
}
New-Item -ItemType Directory -Path $folderName | out-null
$folderName
}
$pwd = ConvertTo-SecureString -String "" -AsPlainText -Force
$cred = new-object System.Management.Automation.PSCredential("",$pwd)
$mb = New-EWSMailboxConnection -Credential $cred -exchangeVersion Exchange2010_SP2
# Optionally specify a mailbox directly to open
# $mb = New-EWSMailboxConnection -Credential $cred -exchangeVersion Exchange2010_SP2 -alternateMailboxSMTPAddress "this@that.com"
$mailCollection = $mb | Read-EWSEmail -FolderName "" -SearchField From -SearchString "" -SearchAlgorithm ContainsString -readMailFilter All | ? {$_.HasAttachments -eq $true}
foreach($mail in $mailCollection)
{
foreach($attachment in $mail.Attachments)
{
if($attachment.Name.ToLower().EndsWith('.xml'))
{
$folderName = Generate-RandomFolder
$attachName = $attachment.Name
$attachPath = "$folderName\$attachName"
$attachment.Load("$folderName\$attachName")
#Process the downloaded XML file
Remove-Item $attachPath
Remove-Item $folderName
}
}
}
Advertisements
Leave a Reply