Fun with Exchange

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

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

$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 ""
$mailCollection = $mb | Read-EWSEmail -FolderName "" -SearchField From -SearchString "" -SearchAlgorithm ContainsString -readMailFilter All | ? {$_.HasAttachments -eq $true}

foreach($mail in $mailCollection)
    foreach($attachment in $mail.Attachments)
            $folderName = Generate-RandomFolder
            $attachName = $attachment.Name
            $attachPath = "$folderName\$attachName"
            #Process the downloaded XML file
            Remove-Item $attachPath
            Remove-Item $folderName

Posted in Workflow

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: