How does one specify a shared email account during a PowerShell script? I'm trying to get metadata from a shared email account using this code (designed for a default email account):
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")
$mailboxRoot = $mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent
$results = Get-MailFromOutlookFolder -ParentFolder $mailboxRoot
$results | Export-Csv -Path "C:\Temp\email.csv"
I've made four attempts to solve this problem:
1.
$mailboxRoot = $mapi.Folder(1).Parent
(In place of the line $mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent)
Error code: Method invocation failed because [Microsoft.Office.Interop.Outlook.NameSpaceClass] does not contain a method named 'GetFolder'.
- Based on the info here.
Set-Location -Path 'C:\Users\<username>\AppData\Local\Microsoft\Outlook\<emailaddressOfSharedAccount>'
Error code: Set-Location : Cannot find path (Path definitely exists).
- Based on the info here.
Set FldrIn = FldrInbox.Folders("C:\Users\<userName>\AppData\Local\Microsoft\Outlook\<sharedInboxEmailAddress>.ost")
Error code:Set-Variable : A positional parameter cannot be found that accepts argument 'FldrInbox.Folders'.
- Based on the info here.
GetSharedDefaultFolder (Microsoft.Office.Interop.Outlook.Recipient Recipient, Microsoft.Office.Interop.Outlook.OlDefaultFolders FolderType)
Error code: 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.
Update: It seems like I need to update the code here.