Reputation: 88
In Office365, I have setup a Shared Mailbox with a calendar called "Corp Calendar", which is shared to any users who need access to the Corporate Calendar. I have full permissions to the mailbox, so it opens alongside mine in Outlook.
I have created a PowerShell script that checks the calendar for new or changed entries, and then sends me an email with the details. Sometimes, it will fail because it can't find the specified folder.
These three lines execute fine:
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
This is the line that fails intermittently:
$current = $namespace.Folders.Item('Corporate Calendar').Folders.Item('Calendar').Folders.Item('Corp Calendar').Items
You can see the structure it is digging into: Corporate Calendar Mailbox > Calendar > Corp Calendar
Here is the error that PowerShell returns:
Exception calling "Item" with "1" argument(s): "The attempted operation failed. An object could not be found." At C:\Users\aaldrich\desktop\Scripts\Calendar.ps1:72 char:1
+ $current = $namespace.Folders.Item('Corporate Calendar').Folders.Item('Calendar' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation
Once when it did this, I dug into the namespace, and it seems to be able to see every mailbox except for the Corporate Calendar mailbox. Restarting Outlook fixes the issue. The next time this happens, I will go in and check again.
I have also implemented a Send/Receive before checking for new items ($namespace.SendAndReceive(1)
), but adding that did not change the frequency of the error. The error seems to happen most when the calendar is edited by other people, but that is not very consistent.
Right now the script only sends email to me, and will also send me an email with any errors. Eventually, though, I want it to email everyone automatically.
Unimportant Side Note: Some of you may wonder "Why not just use public folders with the folder assistant?" Forwarding intact won't work for our Exchange organization (MS has been looking at it for months...). Forwarding as attachment forwards in a way that cannot be viewed from cell phones. Standard forwarding is useless for calendar events.
Thanks!
Edit: I just got it to fail again. Unfortunately, I was able to run the failing line again and it worked just fine. That is not what happened before. Could it have these issues if a Send/Receive was going on while I checked for items? I've been giving it 15 seconds to complete the Send/Receive... I'll increase that length.
Upvotes: 0
Views: 537
Reputation: 66215
Your code assumes that the mailbox 'Corporate Calendar' was already added to the user's profile as a delegate store. First of all, the mailbox name might be different (e.g. '[email protected]'), secondly it might not be there at all.
Why not use Namespace.GetSharedDefaultFolder to open the default calendar folder from that mailbox?
Upvotes: 0