The recursive selection of directories from Outlook

rated by 0 users
This post has 1 Reply | 2 Followers

Top 50 Contributor
Posts 80
Bush Posted: 02-08-2012 9:40 AM

Hi All.

I have found how to receive the list of directories from Outlook here. I have wanted to rewrite this code more compactly and have made it so:

function get-mailfolders {            
    $outlookfolders = @()
    $outlook = New-Object -ComObject Outlook.Application
    $outlook.Session.Folders | ForEach-Object {$_.Folders} | 
    ForEach-Object {$outlookfolders += New-Object PSObject -Property @{            
        Path = $($_.FullFolderPath); EntryID = $($_.EntryID); StoreID = $($_.StoreID)}            
    }
    $outlookfolders       
}

The result turns out identical, but in both cases there is a problem: nested directories aren't considered. I have changed the code so:

function get-mailfolders2 {            
    $outlookfolders = @()
    $outlook = New-Object -ComObject Outlook.Application
    $outlook.Session.Folders | ForEach-Object {$_.Folders} | ForEach-Object { $outlookfolders += get-subfolders $_ }
    $outlookfolders       
}
# The recursive handling  function get-subfolders ($folder) { $array = New-Object PSObject -Property @{ Path = $($folder.FullFolderPath); EntryID = $($folder.EntryID); StoreID = $($folder.StoreID)} $folder.Folders | ForEach-Object $array += get-subfolders $_ }
$array
}

I run it:

get-mailfolders2 | Select-Object -Property Path

But I get error message:

Error at performance of a script because of overflow of depth of calls. Depth of a call has reached 1001 And the maximum value 1000. At line:0 char:0

Where my error? In get-subfolders function, for $folder.Folders property, I receive empty value always. Why?

Regards

 

Top 25 Contributor
Posts 345
Top Contributor

I see this query remains unanswered.

The issue here is mainly to do with how the Outlook COM object actually works. In particular, look at and perhaps use PowerShell Plus professional or the iSE to step through the Get-Subfolders function to examine why the look does not stop recursing. 

You might also chat over on some of the Outlook and Exchange forums to talk to thers who hae done this sort of stuff perhaps using VB. They will know the COM object model a LOT better than I do!

And post an update when you get a fix!

 

Page 1 of 1 (2 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.