在 PowerShell 脚本期间指定共享收件箱

问题描述 投票:0回答:1

如何在 PowerShell 脚本中指定共享电子邮件帐户?我正在尝试使用此代码(专为默认电子邮件帐户设计)从共享电子邮件帐户获取元数据:

Set-StrictMode -Version "Latest"
$ErrorActionPreference = "Stop"

function Get-MailFromOutlookFolder
{
    [CmdletBinding()]
    param 
    (
        [Parameter(Mandatory = $true)]
        [Object] $ParentFolder
    )

    $items = @()

    foreach ($folder in $ParentFolder.Folders)
    {
        foreach ($item in ($folder | Select-Object -ExpandProperty "Items"))
        { 
            if ($item.Class -eq 43)
            {
                # process email
                $items += $item | Select-Object -Property "ConversationTopic", "ReceivedTime", @{ "Label" = "Folder"; "Expression" = { $_.Parent.Name } }
            }
        }

        # process (sub)folder items
        $items += Get-MailFromOutlookFolder -ParentFolder $folder
    }

    return $items    
}

$outlook     = New-Object -Com "Outlook.Application"
$mapi        = $outlook.GetNamespace("MAPI")
$mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent

$results = Get-MailFromOutlookFolder -ParentFolder $mailboxRoot
$results | Export-Csv -Path "C:\Temp\email.csv"

我已经尝试了四次来解决这个问题:

1.

$mailboxRoot = $mapi.Folder(1).Parent

(代替线

$mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent

错误代码:

Method invocation failed because [Microsoft.Office.Interop.Outlook.NameSpaceClass] does not contain a method named 'GetFolder'.

  1. 基于此处的信息。

Set-Location -Path 'C:\Users\<username>\AppData\Local\Microsoft\Outlook\<emailaddressOfSharedAccount>'

错误代码:

Set-Location : Cannot find path 
(路径肯定存在)。

  1. 基于此处的信息。

Set FldrIn = FldrInbox.Folders("C:\Users\<userName>\AppData\Local\Microsoft\Outlook\<sharedInboxEmailAddress>.ost")

错误代码:

Set-Variable : A positional parameter cannot be found that accepts argument 'FldrInbox.Folders'.

  1. 基于此处的信息。

GetSharedDefaultFolder (Microsoft.Office.Interop.Outlook.Recipient Recipient, Microsoft.Office.Interop.Outlook.OlDefaultFolders FolderType)

错误代码:

Microsoft.Office.Interop.Outlook.Recipient : The term 'Microsoft.Office.Interop.Outlook.Recipient' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

更新: 看来我需要更新代码这里

powershell email outlook
1个回答
0
投票

未声明此变量:

$mailboxRoot

这应该可以解决它:

$mailboxRoot = $mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent
© www.soinside.com 2019 - 2024. All rights reserved.