从Exchange下载包含多个主题的附件

时间:2013-10-17 23:48:11

标签: powershell exchangewebservices

我正在尝试从收件箱中某些主题行的电子邮件中下载附件,然后删除该电子邮件。 总共有十几个不同的主题行。 我想安排这个过程每10分钟左右运行一次,所以我希望将处理开销降到最低..

当前脚本在PowerShell中(基于this),但我不确定如何让它循环通过一堆不同的主题。我可以为每个主题重复整个脚本,但这种方式效率很低.. 我也对非PowerShell替代品持开放态度。

$MailboxName = "mailbox@address.com"
$Subject = @("Subject1")
$downloadDirectory = "c:\temp"


Function FindTargetFolder($FolderPath){
$tfTargetidRoot = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$tfTargetidRoot)

for ($lint = 1; $lint -lt $pfArray.Length; $lint++) {
    $pfArray[$lint]
    $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)
    $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+isEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$pfArray[$lint])
            $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
    if ($findFolderResults.TotalCount -gt 0){
        foreach($folder in $findFolderResults.Folders){
            $tfTargetFolder = $folder               
        }
    }
    else{
        "Error Folder Not Found"
        $tfTargetFolder = $null
        break
    }   
}
$Global:findFolder = $tfTargetFolder
}

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)


$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind

$uri=[system.URI] "https://webmail.company.com.au/EWS/Exchange.asmx"
$service.Url = $uri

FindTargetFolder($ProcessedFolderPath)

$folderid = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$Sfsub = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, $Subject[0])
$Sfha = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true)
$sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);
$sfCollection.add($Sfsub)
$sfCollection.add($Sfha)
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000)
$frFolderResult = $InboxFolder.FindItems($sfCollection,$view)
foreach ($miMailItems in $frFolderResult.Items){
$miMailItems.Subject
$miMailItems.Load()
foreach($attach in $miMailItems.Attachments){
$attach.Load()
    $fiFile = new-object System.IO.FileStream(($downloadDirectory + “\” + (Get-Date -Format "yyMMdd") + "_" + $attach.Name.ToString()), [System.IO.FileMode]::Create)
    $fiFile.Write($attach.Content, 0, $attach.Content.Length)
    $fiFile.Close()
    write-host "Downloaded Attachment : " + (($downloadDirectory + “\” + (Get-Date -Format "yyMMdd") + "_" + $attach.Name.ToString()))
}
$miMailItems.isread = $true
$miMailItems.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
$miMailItems.delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::MoveToDeletedItems)
}

1 个答案:

答案 0 :(得分:2)

好的,这就是我所拥有的:

$Subjects = @(
              'newIM_IPSC',
              'newCM_IPSC',
              'CNSDI',
              'SMEC_Incident_SLM',
              'Incident_SLM',
              'MEC_Incident_WorkInfo',
              'SMEC_Incident_Header',
              'SMEC_SR_Header'
            )

[regex]$SubjectRegex = ‘^(?i)(‘ + (($Subjects |foreach {[regex]::escape($_)}) –join “|”) + ‘)$’

然后:

  foreach($attach in $miMailItems.Attachments){

    foreach ($miMailItems in $frFolderResult.Items){
         if ($miMailItems.Subject -match $SubjectRegex)
           {
            $miMailItems.Load()
            foreach($attach in $miMailItems.Attachments){
              $attach.Load()
              $fiFile = new-object System.IO.FileStream(($downloadDirectory + “\” + (Get-Date -Format "yyMMdd") + "_"  + $attach.Name.ToString()), [System.IO.FileMode]::Create)
              $fiFile.Write($attach.Content, 0, $attach.Content.Length)
              $fiFile.Close()
              write-host "Downloaded Attachment : " + (($downloadDirectory + “\” + (Get-Date -Format "yyMMdd") + "_" + $attach.Name.ToString()))
              }
            $miMailItems.isread = $true
            $miMailItems.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
            $miMailItems.delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::MoveToDeletedItems)
           }
         }

根据需要在$ Subjects数组中添加或删除主题行。

可以在此处找到构建正则表达式的位的说明: http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/18/speed-up-array-comparisons-in-powershell-with-a-runtime-regex.aspx