Mit Powershell E-Mails aus Outlook lesen

Momentan bastel ich noch an einem Skript, das mir E-Mails mit allen Attachments sauber auf die Festplatte speichert. Da mir noch wesentliche Kenntnisse der Outlook Architektur fehlen, ist es nicht ganz einfach. Im Moment scheitere ich noch daran, rekursiv durch alle Ordner durchzugehen und die Anzahl der enthaltenen E-Mails auszugeben. Folgendes Skript macht das nur bis zur zweiten Ebene.

$outlook = new-object -com Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
$inbox = $outlook.Session.GetDefaultFolder(6)
 
# iterate through folders
foreach ($folder in $namespace.Folders) {
    Write-Host $folder.name, $folder.count
 
foreach ($subfolder in $folder.Folders) {
    Write-Host "  >" $subfolder.name
 
}}

Die Anzahl der E-Mail bekomme ich für die Unterordner raus:

$outlook = new-object -com Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
 
# iterate through folders
foreach ($folder in $namespace.Folders) {
    Write-Host " " 	$folder.name " enthält " $folder.Items.Count 
 	foreach ($subfolder in $folder.Folders) {
    	Write-Host "   >" 	$subfolder.name " enthält " $subfolder.Items.Count 
}}

Dank Hilfe aus dem Internet habe ich jetzt folgenden funktionierenden Code:

$outlook = new-object -com Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
$inbox = $outlook.Session.GetDefaultFolder(6)
 
function Get-MailboxFolder($folder)
{   
    "{0}: {1}" -f $folder.name, $folder.items.count
 
    foreach ($f in $folder.folders)     {       
        Get-MailboxFolder $f       
    }   
}
 
# iterate through folders
foreach ($folder in $namespace.Folders) {
   Get-MailboxFolder $folder 
}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website