温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Outlook Emails ReceivedTime Out of Order in Powershell

其他 - Powershell中Outlook电子邮件ReceivedTime出现故障

发布于 2020-03-27 10:49:08

长期潜伏者和初次张贴者,因此在这里我会尽量具体一点!

我正在编写的脚本的目标是从excel文件中获取开始和结束日期,并使用该范围从该范围中提取来自Outlook的最新电子邮件。这些电子邮件中的信息然后用于在电子表格中进行报告。

该脚本按预期的方式工作,原因是某些日期的ReceivedTime属性不正确,因此它会抢先获取最早的条目,而不是最新的日期。

If I type $Emails.ReceivedTime into the console, it will show them out of order which is causing the issue. If I type $Emails.ReceivedTime | sort, the issue goes away. My problem is that no matter where I've tried plugging this into my script, it will just hang and never finish execution.

Function GetEmails ()
{
    Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
    $Outlook = New-Object -ComObject Outlook.Application
    $Namespace = $Outlook.GetNameSpace("MAPI")
    $Inbox = $Namespace.Folders.Item('UsernameHere').Folders.Item('Inbox').Items
    return $Inbox
}

Function GetXYZ($Emails) {

    $XYZ = $null
    foreach($Email in $Emails) {   
        if ($Email.SenderEmailAddress -ne $null -and $Email.SenderEmailAddress.Contains('xyz@123.com')) {  
            if (($Email.ReceivedTime -le $end_date) -and ($Email.ReceivedTime -ge $start_date)) {  

                $XYZ = New-Object psobject -Property @{'Sender' = $Email.SenderEmailAddress; 'Time' = $Email.ReceivedTime; 'Body' = $Email.Body}                  
            }

        }
    }

    #Split the body of the email into an array and remove whitespace from the array.
    $Array = $XYZ.Body.Split("`r`n",[System.StringSplitOptions]::RemoveEmptyEntries)

    #Create an array of numeric values only
    $numArray = @()
    $numArray += $Array -replace '[^.0-9]'

    $junk1, [int]$Item1, [int]Item2, [int]$Item3, $junk2 = $numArray

    return New-Object psobject -Property @{'Name' = 'XYZ'; 'Time' = $XYZ.Time; 'Item1' = $Item1.toString("N0"); 'Item2' = $Item2.toString("N0"); 'Item3' = $Item3.toString("N0")}
}


$Emails = GetEmails

GetXYZ($Emails)

#Rest of the code runs here to do things unrelated to my problem...

This is the first 10 email ReceivedTimes currently:

Thursday, May 23, 2019 7:22:19 AM

Thursday, May 23, 2019 6:55:07 AM

Thursday, May 23, 2019 6:22:18 AM

Thursday, May 23, 2019 6:03:07 AM

Thursday, May 23, 2019 6:02:21 AM

Thursday, May 23, 2019 5:22:17 AM

Thursday, May 23, 2019 4:22:03 AM

Thursday, May 23, 2019 3:55:07 AM

Thursday, May 23, 2019 3:22:18 AM

Thursday, May 23, 2019 3:01:33 AM

This is what the first 10 lines SHOULD be:

Wednesday, April 3, 2019 9:00:07 AM

Thursday, April 4, 2019 9:00:04 AM

Friday, April 5, 2019 9:00:08 AM

Saturday, April 6, 2019 5:03:22 AM

2019年4月6日,星期六5:11:22 AM

2019年4月6日,星期六5:22:29 AM

2019年4月6日,星期六7:08:59 AM

2019年4月6日,星期六8:11:08 AM

2019年4月6日,星期六8:55:45 AM

2019年4月6日,星期六9:00:02 AM

如果我在这里遗漏了任何重要内容,请随时让我知道,并在此先感谢!

查看更多

查看更多

提问者
whitehudson
被浏览
107
Dmitry Streblechenko 2019-07-04 00:10

如果您从未致电Items.Sort,您不应期望任何特定的订单。在您的特定情况下,如果您要对ReceivedTime属性对Items集合进行排序,则应调用Items.Sort("[ReceivedTime]", true)

另外,正如Eugene所述,如果您仅处理项目的子集,则必须在服务器端使用Items.Restrict进行过滤Items.Find/FindNext在您的特定情况下,Items.Restrict可能会更合适。

发布
问题

分享
好友

手机
浏览

扫码手机浏览