Exchange 服务器上的 Powershell 脚本 returns 62000 条记录中有 800 多条记录。为什么?

Powershell script on Exchange server returns 800+ records out of 62000. Why?

我已经使用下面的脚本从邮箱中提取所有 *.pdf 附件(只需执行一次)。这是发送报告的邮箱,因此它只包含具有特定名称和附件的邮件。
而且它 returns 在 60 多条记录中只有 800 多条记录。任何建议如何修改它? 我尝试将 ItemView 从 1000 更改为 63000,但没有任何变化。

# Name of the mailbox to pull attachments from
$MailboxName = 'maibox@domain.com'
 
# Location to move attachments
$downloadDirectory = 'E:\ToExport'
 
# Path to the Web Services dll
$dllpath = "E:\Exchange\V15\Bin\Microsoft.Exchange.WebServices.dll"
[VOID][Reflection.Assembly]::LoadFile($dllpath)
 
# Create the new web services object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2019)
 
# Create the LDAP security string in order to log into the mailbox
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind
 
# Auto discover the URL used to pull the attachments
$service.AutodiscoverUrl($aceuser.mail.ToString())
 
# Get the folder id of the Inbox
$folderid = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
 
# Find mail in the Inbox with attachments
$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($Sfha)
 
# Grab all the mail that meets the prerequisites
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(63000)
$frFolderResult = $InboxFolder.FindItems($sfCollection,$view)
 
# Loop through the emails
foreach ($miMailItems in $frFolderResult.Items){
 
    # Load the message
    $miMailItems.Load()
 
    # Loop through the attachments
    foreach($attach in $miMailItems.Attachments){
 
        # Load the attachment
        $attach.Load()
 
        # Save the attachment to the predefined location
        $fiFile = new-object System.IO.FileStream(($downloadDirectory + “\” + $attach.Name.ToString()), [System.IO.FileMode]::Create)       
                $fiFile.Write($attach.Content, 0, $attach.Content.Length)
        $fiFile.Close()
    }
 }

Microsoft.Exchange.WebServices.Data.ItemView()

最多只接受 1000,在 $InboxFolder.FindItems($sfCollection,$view) 的响应中,您将得到一个布尔值 属性 的“MoreAvailable”,您可以将 offset 增加 1000 并迭代直到 MoreAvailableFalse.

请查看您在下面编辑的脚本以纳入此内容(第 32、33、38-43 行包括注释和更改):

# Name of the mailbox to pull attachments from
$MailboxName = 'maibox@domain.com'
 
# Location to move attachments
$downloadDirectory = 'E:\ToExport'
 
# Path to the Web Services dll
$dllpath = "E:\Exchange\V15\Bin\Microsoft.Exchange.WebServices.dll"
[VOID][Reflection.Assembly]::LoadFile($dllpath)
 
# Create the new web services object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2019)
 
# Create the LDAP security string in order to log into the mailbox
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind
 
# Auto discover the URL used to pull the attachments
$service.AutodiscoverUrl($aceuser.mail.ToString())
 
# Get the folder id of the Inbox
$folderid = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
 
# Find mail in the Inbox with attachments
$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($Sfha)
 
# Grab all the mail that meets the prerequisites
$frFolderResults = @() # Create array
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000)
$frFolderResult = $InboxFolder.FindItems($sfCollection,$view)
$frFolderResults += $frFolderResult    


### Start While loop to include offset +1000 until $frFolderResult.MoreAvailable is false
While ($frFolderResult.MoreAvailable){
    $view.offset += 1000
    $frFolderResult = $InboxFolder.FindItems($sfCollection,$view)
    $frFolderResults += $frFolderResult
}    

 
# Loop through the emails
foreach ($miMailItems in $frFolderResults.Items){
 
    # Load the message
    $miMailItems.Load()
 
    # Loop through the attachments
    foreach($attach in $miMailItems.Attachments){
 
        # Load the attachment
        $attach.Load()
 
        # Save the attachment to the predefined location
        $fiFile = new-object System.IO.FileStream(($downloadDirectory + “\” + $attach.Name.ToString()), [System.IO.FileMode]::Create)       
                $fiFile.Write($attach.Content, 0, $attach.Content.Length)
        $fiFile.Close()
    }
 }