【发布时间】: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