代码之家  ›  专栏  ›  技术社区  ›  mkingsbu

通过Powershell获取Outlook中最旧的项目

  •  0
  • mkingsbu  · 技术社区  · 6 年前

    我正在做一件事,从我的桌面Outlook应用程序中提取信息。它适用于我试用过的大多数文件夹,但对于一些有近十年电子邮件的文件夹,我会遇到一个“获取‘ReceivedTime’的异常:“内存不足,无法继续执行程序。”这就是我要尝试的:

    # New Outlook object
    $ol = new-object -comobject "Outlook.Application";
    
    # MAPI namespace
    $mapi = $ol.getnamespace("mapi");
    
    # Folder/Inbox
    $folder =  $mapi.Folders.Item('name@email.com').Folders.Item('Inbox')
    
    # Sort by the Received Time
    $contents = $folder.Items | sort ReceivedTime
    
    # Get the first element in the array, convert to JSON, and then output to file
    echo $contents[0] | convertTo-Json | Out-File C:\Users\ME\outlook_1.json -Encoding UTF8
    

    有更好的方法吗?我在Powershell 5.1上。

    编辑:我也尝试过这个方法,在数组中循环,然后在第一个实例中中断,但收到了相同的错误:

    # New Outlook object
    $ol = new-object -comobject "Outlook.Application";
    
    # MAPI namespace
    $mapi = $ol.getnamespace("mapi");
    
    # Folder/Inbox
    $folder =  $mapi.Folders.Item('name@email.com').Folders.Item('Inbox')
    
    # Sort by the Received Time
    $contents = $folder.Items | sort ReceivedTime
    
    $i = 1
    foreach($item in $contents){
        if (-not ([string]::IsNullOrEmpty($item))){
            echo $item | convertTo-Json | Out-File Out-File C:\Users\ME\outlook_1.json -Encoding UTF8-Encoding UTF8
            Break
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   fourpastmidnight    6 年前

    使用 Items.Sort("ReceivedTime", false) ,然后使用 Items(1) .

    一定要去商店 Items 变量中的集合,而不是访问 MAPIFolder.Items 多次,否则你会得到一个全新的 项目 每次你这么做的时候都要反对。

    编辑:我是这个问题的负责人,我在这里为那些可能和我一样高深莫测的人提供了正确的代码,但他们一开始并没有意识到我在说什么!

    # New Outlook object
    $ol = new-object -comobject "Outlook.Application";
    
    # MAPI namespace
    $mapi = $ol.getnamespace("mapi");
    
    $folder =  $mapi.Folders.Item('name@gmail.com').Folders.Item('Inbox')
    
    # Get the items in the folder
    $contents = $folder.Items
    
    # Sort the items in the folder by the metadata, in this case ReceivedTime
    $contents.Sort("ReceivedTime")
    
    # Get the first item in the sorting; in this case, you will get the oldest item in your inbox.
    $item = $contents.GetFirst()
    echo $item
    
    # If instead, you wanted to get the newest item, you could do the same thing but do $item = $contents.GetLast()