遍历outlook邮箱中的文件夹,获取powershell中的所有项目



我是powershell的新手,我正在尝试递归地获取存档中的所有项目和子文件夹中的项目与指定邮箱中的路径。

我真的很感激你的帮助。正如我之前所说,我是powershell和脚本的新手,所以我的代码在一段时间内是不可行的。 下面是我的代码:"‘Clear-HostAdd-Type -assembly "Microsoft.Office.Interop.Outlook"$Outlook = New-Object -comobject Outlook。应用程序$namespace = $Outlook.GetNameSpace("MAPI"
Write-Host "Démarrage du script . . . ."

$Year = Get-Date -Format ("yyyy")
$YearMonth = Get-Date -Format ("yyyyMM")
$Mailbox = $NameSpace.Folders.Item('spam@exemple.fr').Folders.Item('Archive').Folders.Item($Year).Folders.Item($YearMonth)
$Subfolder1 = $NameSpace.Folders.Item('spam@exemple.fr').Folders.Item('Archive').Folders.Item($Year).Folders.Item($YearMonth).folders.item(1)
$Subfolder2 = $NameSpace.Folders.Item('spam@exemple.fr').Folders.Item('Archive').Folders.Item($Year).Folders.Item($YearMonth).folders.item(2)
$Subfolder3 = $NameSpace.Folders.Item('spam@exemple.fr').Folders.Item('Archive').Folders.Item($Year).Folders.Item($YearMonth).folders.item(3)
$Subfolder4 = $NameSpace.Folders.Item('spam@exemple.fr').Folders.Item('Archive').Folders.Item($Year).Folders.Item($YearMonth).folders.item(4)

$Subfolder4sub1 = $NameSpace.Folders.Item('spam@exemple.fr').Folders.Item('Archive').Folders.Item($Year).Folders.Item($YearMonth).folders.item(4).folders.item(1)

$Subfolder4sub2 = $NameSpace.Folders.Item('spam@exemple.fr').Folders.Item('Archive').Folders.Item($Year).Folders.Item($YearMonth).folders.item(4).folders.item(2)
$Subfolder4sub3 = $NameSpace.Folders.Item('spam@exemple.fr').Folders.Item('Archive').Folders.Item($Year).Folders.Item($YearMonth).folders.item(4).folders.item(3)

Write-Host "Traitement des mails . . . ."
$CSVfolder = "C:TEMPdatas_spamV3.csv"
Clear-Content "C:TEMPdatas_spamV3.csv"
$headers = "Id;Emplacement;Date;Emetteur;Sujet" | Add-Content -Path $CSVfolder

$Mailbox.Items | foreach {
$Id = New-Guid
$path = $Mailbox.FullFolderPath
$datas = "$($_.ReceivedTime);$($_.SenderEmailAddress);$($_.Subject)"
"$Id;$path;$datas" | Add-Content -Path $CSVfolder
}
$Subfolder1.Items | foreach {
$Id = New-Guid
$path = $Subfolder1.FullFolderPath
$datas = "$($_.ReceivedTime);$($_.SenderEmailAddress);$($_.Subject)"
"$Id;$path;$datas" | Add-Content -Path $CSVfolder
}
$Subfolder2.Items | foreach {
$Id = New-Guid
$path = $Subfolder2.FullFolderPath
$datas = "$($_.ReceivedTime);$($_.SenderEmailAddress);$($_.Subject)"
"$Id;$path;$datas" | Add-Content -Path $CSVfolder
}
$Subfolder3.Items | foreach {
$Id = New-Guid
$path = $Subfolder3.FullFolderPath
$datas = "$($_.ReceivedTime);$($_.SenderEmailAddress);$($_.Subject)"
"$Id;$path;$datas" | Add-Content -Path $CSVfolder
}
$Subfolder4sub1.Items | foreach {
$Id
$path = $Subfolder4sub1.FullFolderPath
$datas = "$($_.ReceivedTime);$($_.SenderEmailAddress);$($_.Subject)"
"$Id;$path;$datas" | Add-Content -Path $CSVfolder
}
$Subfolder4sub2.Items | foreach {
$Id = New-Guid
$path = $Subfolder4sub2.FullFolderPath
$datas = "$($_.ReceivedTime);$($_.SenderEmailAddress);$($_.Subject)"
"$Id;$path;$datas" | Add-Content -Path $CSVfolder
}
$Subfolder4sub2.Items | foreach {
$Id = New-Guid
$path = $Subfolder4sub3.FullFolderPath
$datas = "$($_.ReceivedTime);$($_.SenderEmailAddress);$($_.Subject)"
"$Id;$path;$datas" | Add-Content -Path $CSVfolder
}
'''

编辑:我自己找到了解决方案:"‘clear-host$outlook = New-Object -Com outlook。应用程序$mapi = $outlook.GetNamespace(' mapi ')$ mailboxRoot =NameSpace.Folders.Item美元(spam@mail.fr) .Folders.Item(存档)$yearmonth = Get-Date -Format (" yyyym")

Clear-Content "C:TEMPOut_SPAM.csv"
$headers = "Id;Emplacement;Date;Emetteur;Sujet" | Add-Content -Path 
"C:TEMPOut_SPAM.csv"
$walkFolderScriptBlockBis = {
param(
$currentFolder
)
foreach ($item in $currentFolder.Folders) {
$item.Items() | Foreach { 
$Id = $_.ConversationId
$path = $item.FullFolderPath
$datas = "$($_.ReceivedTime);$($_.SenderName);$($_.Subject)"
"$Id;$path;$datas" | Add-Content -Path "C:TEMPOut_SPAM.csv"
}

& $walkFolderScriptBlockBis $item


}
}
$walkFolderScriptBlock = {
param(
$currentFolder,$index
)
foreach ($item in $currentFolder.Folders) {
if($index -lt 3){
#$item.FolderPath
$yearmonth = Get-Date -Format ("yyyyMM")
if ($item.FolderPath.Contains($yearmonth)) {
& $walkFolderScriptBlockBis $item

}
$temp = $index + 1
& $walkFolderScriptBlock $item $temp
}

}
}
$index = 0
& $walkFolderScriptBlock $mailboxRoot $index

"

相关内容

  • 没有找到相关文章

最新更新