【问题标题】:Checking number of unread exchange mails with EWS使用 EWS 检查未读交换邮件的数量
【发布时间】:2017-10-24 22:05:59
【问题描述】:

我是使用 PowerShell 的新手,目前正在编写一个脚本来检查共享邮箱是否包含未读邮件。 我目前正在尝试使用FindItems() 方法取回我的邮件。 这是我的代码:

[int]$nb_unread = 0
[string]$email = "user@domain.org"
[string]$Msg = ""
[int]$Code = 0
[string]$err = ""
Try
{

    Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
    $ews = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013)

    $ews.Credentials = New-Object Net.NetworkCredential('user', 'password')
    $ews.AutodiscoverUrl($email, {$true})
    $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($ews,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
    $view = new-object Microsoft.Exchange.WebServices.Data.ItemView(10)
    $mailItems = $inbox.FindItems($view)
    $mails | ForEach {$_.Load()}

    foreach($mail in $mails)
    {
        if($mail.isread -eq $false)
        {
            nb_unread += 1
        }
    }
    if (nb_unread -eq 0)
    {
        $Msg = "OK;No unread mail."
    }
    else
    {
        $Msg = ("NOOK;Unread mails : " -f $nb_unread)
    }
}
Catch
{
    $Code = 2
    $Msg = ( "CRITICAL: erreur(s) d'execution du script : {0}" -f $err )
}

当我的脚本执行 '$mailItems = $inbox.FindItems($view)' 行时出现此错误。

Exception lors de l'appel de «FindItems» avec «1» argument(s): «The request failed. Le serveur distant a retourné une
erreur: (501) Non implémenté.»
Au caractère Ligne:16 : 5
+     $mailItems = $inbox.FindItems($view)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServiceRequestException

粗略的英文翻译

Exception when calling "FindItems" with "1" argument (s): "The request failed. The remote server returned an error: (501) Not implemented. 
At Line:16 Char:5 
+ $ mailItems = $inbox.FindItems ($ view) 
+ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo: NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId: ServiceRequestException

【问题讨论】:

  • 你的问题究竟是什么?这段代码怎么不起作用?您有一个巨大的 try 块并且没有应该触发语法错误的 catch 块。代码是否从一个空的捕获中默默地出错?
  • 好吧,我的脚本在执行 FindItem 时崩溃了。我的消息已更新为错误。

标签: powershell exchangewebservices powershell-5.0


【解决方案1】:

A related question in C# 与您一脉相承。您无需查询邮箱中的每封邮件以查看是否未读。有一个数据文件夹属性已经为您提供了该信息:UnreadCount

# Get the Mailbox ID of the Inbox folder.
$inboxFolderID = [Microsoft.Exchange.WebServices.Data.FolderId]::new([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailAddress)

# Bind to the inbox folder.
$boundFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($objExchange,$inboxFolderID)
$boundFolder.UnreadCount

在您的情况下,您应该只需要使用 $inbox.UnreadCount 并删除您的循环逻辑。

【讨论】:

  • 谢谢,$inbox.UnreadCount 调用可以满足我在脚本中的需要。
【解决方案2】:

我采用不同的方法:

$inbox_mails = $Inbox.FindItems($Inbox.TotalCount)
$inbox_mails | where-object IsRead -eq $false

然后将其通过管道传输到格式表中,或者将其包装到 ().count 中,或者用它做其他事情。

喜欢:

($inbox_mails | where-object IsRead -eq $false).count

另外,你的$sfCollectionvariable 来自哪里?

【讨论】:

  • 这是一个非常糟糕的方法。您实际上是在请求当前文件夹中的所有邮件。很适合小邮箱。对于一个包含数千个项目的邮箱来说是一场灾难。
  • 是的,我现在知道了,直到我看到另一个答案,我才意识到 UnreadCount 属性。
猜你喜欢
  • 2013-12-05
  • 2010-10-31
  • 2011-09-16
  • 2013-12-28
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
相关资源
最近更新 更多