2017-05-24 6 views
1

저는 PowerShell을 처음 사용하고 있으며 공유 편지함에 읽지 않은 메일이 있는지 확인하기 위해 현재 스크립트 작업을하고 있습니다. 현재 FindItems() 방법으로 메일을 다시 보내려고합니다.EWS로 읽지 않은 교환 메일 수를 확인하십시오.

[int]$nb_unread = 0 
[string]$email = "[email protected]" 
[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 ($보기)'라인을 실행할 때이 오류가 발생했습니다 : 다음은 내 코드입니다.

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 
+0

그리고 당신의 문제는 정확히 무엇입니까? 이 코드는 어떻게 작동하지 않습니까? 거대한 try 블록과 catch 블록이 없으므로 구문 오류가 발생합니다. 코드가 빈 catch에서 자동으로 오류가 발생합니까? – Matt

+0

글쎄, FindItem을 실행할 때 스크립트가 충돌합니다. 내 메시지가 오류로 업데이트됩니다. – Aurelien

답변

2

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_mails = $Inbox.FindItems($Inbox.TotalCount) 
$inbox_mails | where-object IsRead -eq $false 

그리고 파이프 중 하나 형식으로 테이블을, 또는 그것으로 뭔가를 ().count로 포장 또는 수행

+0

감사합니다. $ inbox.UnreadCount 호출은 스크립트에서 필요한 것을 수행합니다. – Aurelien

0

나는 다른 aproach로 이동합니다.

처럼 :

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

곳은 $sfCollection 변수에서 오는가?

+0

이것은 매우 나쁜 접근입니다. 근본적으로 현재 폴더에있는 모든 메일을 요청하고 있습니다. 작은 우편함에 좋습니다. 수천 개의 항목이있는 사서함에 대한 재난입니다. – bluuf

+0

예. 지금 알고 있습니다. 다른 대답을 보았을 때부터 UnreadCount 속성을 알게되었습니다. – Adamar