【问题标题】:Accessing Outlook Folder in PowerShell 5在 PowerShell 5 中访问 Outlook 文件夹
【发布时间】:2017-01-22 20:08:28
【问题描述】:

我在 PS 4 中编写了一个脚本,它可以读取 Outlook 文件夹的内容、查找未读电子邮件、处理它们,然后将它们移动到另一个文件夹中。在 PS 5 被推送到我的工作站之前,这个脚本运行良好。现在,我收到以下错误:

Error while invoking [PROPERTYGET, DISPID(0)]. Could not find member.
At U:\Powershell\Scripts\Outlook - Process Emails.ps1:214 char:7
+     $($folger.Items)[$i].Subject
+       ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], MissingMemberException
    + FullyQualifiedErrorId : System.MissingMemberException

这是主要的代码块:

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
$NameSpace = $Outlook.GetNameSpace("MAPI")

$folder = $NameSpace.Folders.Item("Scans").Folders.Item("Results")

for ($i = ($folder.Items.Count - 1); $i -ge 0; $i--)
{
    if ($($folder.Items)[$i].UnRead) # It breaks on this line
    {
        # Do stuff
    }
}

两个问题:

1) PS 4 和 PS 5 之间发生了什么变化导致了这种情况? PS 5 的发行说明中没有任何内容。

2) 我可以做些什么来让我的脚本备份并运行而没有问题?

【问题讨论】:

    标签: powershell outlook powershell-4.0 powershell-5.0


    【解决方案1】:

    我自己想出来的...

    if ($($folder.Items)[$i].UnRead)
    

    改为

    if ($folder.Items[$i].UnRead)
    

    我从来不知道为什么需要额外的 $ 和括号,但它在原始版本中有效,所以我从来没有搞砸过。

    【讨论】:

    • 这很有趣。我不知道在 v4 和 v5 之间会发生什么改变来改变它。但我认为原始版本一直都是错误的。可能是已修复的错误?
    • 我最近实际上在 Outlook 中看到了类似的东西,但我使用 select -index 来解决这个问题。
    • @briantist,我根据在 SO:stackoverflow.com/a/24829429/6679564 上找到的代码建模了我的原始代码,这里 TheMadTechnician 使用了一组额外的 $ 和括号,但我从来不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 2012-07-10
    • 2021-12-10
    • 1970-01-01
    • 2015-07-31
    • 2016-06-12
    • 2016-09-28
    • 2010-09-09
    • 1970-01-01
    相关资源
    最近更新 更多