2
votes

My goal for this powershell script is to monitor a particular folder for all the mailboxes in Exchange Online (Office 365) and if an item is created, it will send that item as an email to another smtp address. I can achieve this for just one mailbox without any errors using impersonation.

However, I am trying to create a list of folderid array for each mailbox like:

foreach ($usermailbox in $mboxlist) {
 $farray += Get-TargetF($usermailbox)
 }

$farray contains objects of the type:

Microsoft.Exchange.WebServices.Data.FolderId($tfTargetFolder.Id)

I then look for events in:

$service.SubscribeToPullNotifications($farray,60,$null,[Microsoft.Exchange.WebServices.Data.EventType]::Created)

Note that Get-TargetF function gets the servername through autodiscover for each mailbox. It also uses $service.FindFolders for finding the correct folder.

Running the script like I showed above results in an error for few mailboxes but not all (I had filtered out 5 mailboxes for testing).

New-Object : Constructor not found. Cannot find an appropriate constructor for type Microsoft.Exchange.WebServices.Data.FolderId.
At C:\Documents\mysc.ps1:63 char:22
+ $InboxId = new-object <<<<  Microsoft.Exchange.WebServices.Data.FolderId($tfTargetFolder.Id)
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

And when I start listening to the events, I get this error:

Cannot find an overload for "SubscribeToPullNotifications" and the argument count: "4".
At C:\Documents\mysc.ps1:79 char:59
+  $pullSubscription = $service.SubscribeToPullNotifications <<<< ($farray,60,$null,[Microsoft.Exchange.WebServices.Data.EventType]::Created)
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

I have also not figured out how to deal with the throttling issues with EWS on Exchange Online mentioned here. I suspect the subscription above will timeout after an hour by default.

What is the correct way to achieve this in a single pssession?

1
Have you considered using mailbox Rules?Shay Levy
The items created in the folder I am looking for are not emails. These items (IM's) are generated by Lync IM. The folder is called "Conversation Histroy".fmysky
I'm curious if you've solved this and, if so, have you posted a blog about the solution. It looks very interesting.Kevin Buchan

1 Answers

0
votes

I think the only option you have here is to create separate Subscription for each mailbox and then loop through them one-by-one to call GetEvents to see if there are any new Create events.

As per my understanding, Specifying folder ids from different mailboxes cannot work since impersonation is more about making a call as some other user. If that other user doesn't have delegate privileges to the folder ids mentioned in request, it is bound to fail. May be you should try with Console app instead of Powershell first to quickly see on what really is working. That is to avoid any PS specific errors hindering the investigation of actual problem.

With Streaming Subscription approach, you could group mailboxes based on their server affinity as described here on MSDN. You should look at the trade-offs between Pull vs. Streaming before making the decision in that though.