0

anyone have any idea what I may need to change. I have the code below that works fine. But recently I've decided to hide the mailbox from the address book. This has cause the below script to stop working.. If I unhide the mailbox it works again. But I would prefer to keep it hidden.

quick description of what it does. it gets a shared mailbox then a specific folder in the inbox folder and then gets all the emails in that folder.

$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$olRecipient = $namespace.CreateRecipient("sharedmailbox@mail.com")
$SInbox = $namespace.GetSharedDefaultFolder($olRecipient,"6")
$targetFolder = $SInbox.Folders('targetfolder')
$Completedfolder = $targetFolder.Folders("Complete")
$Emails = @()
$Emails =  $targetfolder.Items
Jonboy89
  • 9
  • 1
  • Have you tested each line in a PowerShell console to see which line fails? Probably a good first step. – cet51 Jun 01 '18 at 15:11
  • Yes it fails at $targetFolder = $Sinbox... If I unhide the box it shows up just fine.. so I don't know if I need to use a different method or retrieve the mailbox in another way.. – Jonboy89 Jun 01 '18 at 15:59
  • Is `$SInbox` empty or does it contain the information you expect it to have? – cet51 Jun 01 '18 at 16:16
  • Variable has the information I expect. I can then dig further down to $_.folders but I get an error An error occurred while enumerating through a collection: Exception from HRESULT: 0xEB44010F. If I check the variable it shows it has objects in it.. but for some reason it can't dive into it or expand it. – Jonboy89 Jun 01 '18 at 16:23
  • Normally enumeration errors come from changing multiple items without having them in an array, but you aren't making any changes here, only grabbing information. Hopefully someone more knowledgable takes a look at your post! – cet51 Jun 01 '18 at 16:30
  • Instead of using the Outlook COM object to retrieve the items I would use the EWS API (provider by Microsoft). This is much faster as the Outlook COM object and you don't encounter these kind of limitations. As an alternative you can setup address lists and address book policies so the mailbox is not visible for normal users but still visible for the account that connects to the mailbox. – bluuf Jun 02 '18 at 08:42

1 Answers1

0

The recipient created by CreateRecipient cannot be resolved if the user is hidden from GAL. And if it cannot be resolved, GetSharedDefaultFolder will not work either.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Do you know of another way I can do this while keeping the Mailbox Hidden from the Gal? Been using some googleFu but no luck so far.. – Jonboy89 Jun 01 '18 at 17:32
  • You can try to use Redemption and its version of RDOSession.GetSharedMailbox - it should be able to work off an SMTP address only, it only needs to retrieve the autodiscover XML. – Dmitry Streblechenko Jun 01 '18 at 17:48
  • I'm not familiar with Redemption, I will look into it thanks! – Jonboy89 Jun 01 '18 at 18:07