Search code examples
powershellmailbox

Browsing secondary mailboxes with PowerShell


As part of my work placement, I need to use PowerShell to browse a mailbox and retrieve information from it. I have no problems with this. However, it is the secondary mailbox that the script has to browse and it only browses the main mailbox. Do you have any ideas? I've included the code just below. Thank you in advance for your answers. Have a nice day.

# Initialise un compteur qui va servir pour la boucle qui gère le stockage
$cpt = 0
$contain = ""
# Connexion à la boite mail + récupération des mails
$olFolderInbox = 6
$outlook = new-object -com outlook.application
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder($outlook)

# Boucle permettant de parcourir la boite mail et de récupérer les informations sur la totalité des mails
$inbox.items | foreach {
    $Expediteur = $_.SenderName
    $Recepteur = $_.To
    $DateReception = $_.ReceivedTime
    $Sujet = $_.Subject
    $Contenu = $_.Body
    $Fichier = $_.Attachments

    # Grâce aux informations obtenus, on peut faire une condition permettant de prendre seulement les mails utiles au script
    if ($Expediteur -Match ('Backup'))
    {
        $contain = $contain + $Contenu + "<br>" + "<br>"
    }
}

# Variables
$to = "private mailbox"
$from = "private mailbox"
$smtpServer = "private SMTP server"
$encodingMail = [System.Text.Encoding]::UTF8
 
# Préparation du message email  
$subject = "Log + message d'erreur"      
$body = "
     Bonjour,
     <p> Voici les logs/erreurs.<br>"
$body = $body + "<br>$($contain)"
$body = $body + "<br>"
$body = $body + "<br><br>Cordialement,<br><br>"
                 
Send-MailMessage -to $to -From $from -Subject $subject -SmtpServer $smtpServer -BodyAsHtml $body -Encoding $encodingMail 

Solution

  • I created following function a while ago that returns folder handle in Outlook, would it be a secondary mbx

    function Get-MapiFolder
    {   [CmdletBinding()]
        Param
        (   # Recipient mailbox to access as per displayed in outlook. If omitted default store is taken
            [Parameter(Mandatory=$False)]
            [string]$Recipient,
            # Folder in mailbox
            [Parameter(Mandatory=$False)]
            [string]$FolderHandle = "Inbox",
            # Folder delimiter, default being /
            [Parameter(Mandatory=$False)]
            [string]$FolderDelimiter = "{/}"
        )
        Begin
        {   write-Verbose ("Running function is: {0} " -f $MyInvocation.MyCommand)
            $OutlookObject = New-Object -comobject outlook.application
            $NameSpace = $OutlookObject.GetNamespace("MAPI")
            if (-not $Recipient)
            {   $Recipient = $NameSpace.DefaultStore.DisplayName
            }
        }
        Process
        {   Write-Verbose "Connecting to mailbox : $Recipient" 
            Write-Verbose "Folder to fetch : $FolderHandle" 
            Write-Verbose "Folder delimiter being : $FolderDelimiter" 
            try
            {   $OutlookFolder = $NameSpace.folders($recipient)
            }
            catch [System.Runtime.InteropServices.COMException]
            {   Write-warning "$recipient is incorrect"
                break
            }
            finally
            {}
    
            foreach ($folder in ($FolderHandle.Split($FolderDelimiter)))
            {   try
                {   $OutlookFolder = $OutlookFolder.folders($folder)
                }
                catch [System.Runtime.InteropServices.COMException]
                {   Write-Warning "Folder $folder does not exit"
                    break
                }
                finally
                {}
            }
        }
        End
        {   return $OutlookFolder
        }
    }
    

    To answer your question, $Recipient parameter must be the display name of the other mailbox, as it appears in outlook. Default folder is Inbox but can be changed. $delimiter is there if needed to dig deeper in the structure.

    You can then parse items property from returned object.