none
Restricting Dates for Outlook Items: Items.Restrict method & Start datetimes RRS feed

  • Question

  • GetOutlookCalendarInformation (https://gallery.technet.microsoft.com/scriptcenter/a20bf555-1e51-4038-9739-36d153deaffa and https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/24/use-powershell-to-export-outlook-calendar-information/) is super handy, but can be very slow.  So want to restrict the results it returns which is much faster than returning all results and iterating through them.  I have reviewed the details for Restrict filtering (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook._items.find?view=outlook-pia#Microsoft_Office_Interop_Outlook__Items_Find_System_String_) but it just doesn't seem to work right

    Start with this code:

    $timediff = New-Timespan -Hours 8
    $windowStart = Get-Date
    $windowEnd = $windowStart + $timediff
    $startString = $windowStart.ToString("MM/dd/yyyy hh:mm tt")
    $endString = $windowEnd.ToString("MM/dd/yyyy hh:mm tt")

    Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null $olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type] $outlook = new-object -comobject outlook.application $namespace = $outlook.GetNameSpace("MAPI") $folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)

    If I don't restrict it to a start and end, I currently get 1473 objects:

    $folder.items  | select-object -Property Start | Measure-Object
    
    Count    : 1473

    If I restrict it to just a start, I get 80 objects, so it is clearly restricting it, but some of the dates are before my start date!

    $startString
    09/04/2018 09:47 AM
    
    
    $folder.items.Restrict("[Start] > " + "'" + $startString + "'")  | select-object -Property Start  
    
    Start                 
    -----                 
    1/18/2018 12:00:00 PM 
    12/10/2018 12:00:00 AM
    9/19/2018 12:00:00 AM 
    (etc)
    
    $folder.items.Restrict("[Start] > " + "'" + $startString + "'")  | select-object -Property Start  | Measure-Object
    
    Count    : 80

    Those with datetimes that precede my range appear to be recurring events; the Start date used for "Restrict" seems to take into account the recurrence, but when displayed it doesn't.

    Next, I put in start and end datetimes. This is when it gets really weird.

    $startString
    09/04/2018 09:47 AM
    
    $endstring
    09/04/2018 05:47 PM
    
    $folder.items.Restrict("[Start] > '" + $startString + "' AND [Start] < '" + $endString + "'")  | select-object -Property Start  
    
    Start                
    -----                
    1/18/2018 12:00:00 PM
    3/27/2018 8:30:00 AM 
    3/20/2018 8:30:00 AM 
    (etc)
    
    $folder.items.Restrict("[Start] > '" + $startString + "' AND [Start] < '" + $endString + "'")  | select-object -Property Start  | Measure-Object
    
    Count    : 27

    So it is further filtering.  But there are items listed that are clearly outside my range of 8 hours.  I see things that are happening tomorrow, and at least one that is 3 weeks out.

    So my questions are:

    1. Why am I seeing items that are outside my restriction range?

    2. For recurring items, how do i get the relevant start date, not the first instance of the recurring item?

    (yes, I know I should use Graph, but the company I am working with hasn't given users permissions to query it.)

    Thanks!

    • Moved by Bill_Stewart Wednesday, December 12, 2018 9:29 PM This is not "debug/fix/rewrite my script for me" forum
    Tuesday, September 4, 2018 2:10 PM