powershell 无法使用PS脚本在共享邮箱中查看来自其他文件夹的电子邮件

eqoofvh9  于 2023-06-29  发布在  Shell
关注(0)|答案(1)|浏览(116)

我有一个脚本来检查我的Outlook收件箱中的邮件。我也可以通过这个脚本检查来自另一个邮箱的邮件:

Function Get-OutlookItems {
    Param (
        [Parameter(Mandatory=$true)]
        [String]$SharedMailbox,
        [Switch]$IncludeSubfolders
    )

    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")

    # Resolve the shared mailbox folder based on the email address
    $sharedMailboxAddressEntry = $namespace.CreateRecipient($SharedMailbox)
    $sharedMailboxAddressEntry.Resolve()

    if ($sharedMailboxAddressEntry.Resolved) {
        $sharedMailboxFolder = $namespace.GetSharedDefaultFolder($sharedMailboxAddressEntry, $olFolders::olFolderInbox)
    } else {
        throw "Unable to resolve the shared mailbox."
    }

    if ($IncludeSubfolders) {
        $folderItems = $sharedMailboxFolder.Items.Restrict
    } else {
        $folderItems = $sharedMailboxFolder.Items
    }

    $folderItems | Select-Object -Property Subject, ReceivedTime, Importance, SenderName
}

Get-OutlookItems -SharedMailbox 'shared@domain.com' -IncludeSubFolders

但它似乎只显示我的收件箱。如何显示此共享邮箱中的所有文件夹?或者这是不可能的?

ldxq2e6h

ldxq2e6h1#

调用Restict方法从子文件夹中获取项目没有任何意义:

$folderItems = $sharedMailboxFolder.Items.Restrict

Items类的Restrict方法(以及Find/FindNext方法)允许获取与指定搜索条件对应的项。这是完全不同的任务。
要获取子文件夹,您需要使用Folders属性,该属性返回表示指定Folder中包含的所有文件夹的Folders集合。下面的示例代码(C#)首先使用GetRootFolder()方法获取默认存储的根文件夹。然后调用根文件夹上的EnumerateFolders方法。EnumerateFolders接受一个根文件夹,并遍历根文件夹所代表的默认存储的文件夹。EnumerateFolders首先使用Folders属性获取根文件夹对象的子文件夹。然后递归调用EnumerateFolders,以枚举层次结构中的所有文件夹。

private void EnumerateFoldersInDefaultStore()
{
    Outlook.Folder root =
        Application.Session.
        DefaultStore.GetRootFolder() as Outlook.Folder;
    EnumerateFolders(root);
}

// Uses recursion to enumerate Outlook subfolders.
private void EnumerateFolders(Outlook.Folder folder)
{
    Outlook.Folders childFolders =
        folder.Folders;
    if (childFolders.Count > 0)
    {
        foreach (Outlook.Folder childFolder in childFolders)
        {
            // Write the folder path.
            Debug.WriteLine(childFolder.FolderPath);
            // Call EnumerateFolders using childFolder.
            EnumerateFolders(childFolder);
        }
    }
}

递归地遍历所有子文件夹,您可能会得到所有项目/文件夹。

相关问题