# --- CONFIGURAZIONE --- $archiveName = "Archivio online - alessandro.battilani@intesasanpaolo.com" $monthsLimit = -8 # ---------------------- try { $outlook = New-Object -ComObject Outlook.Application $namespace = $outlook.GetNamespace("MAPI") $inbox = $namespace.GetDefaultFolder(6) $archiveRoot = $namespace.Folders.Item($archiveName) if ($null -eq $archiveRoot) { Write-Host "ERRORE: Archivio non trovato." -ForegroundColor Red exit } # CALCOLO DATA TAGLIO $cutoffDate = (Get-Date).AddMonths($monthsLimit) Write-Host "OGGI: $((Get-Date).ToShortDateString())" -ForegroundColor White Write-Host "ARCHIVIO TUTTO QUELLO CHE E' PRIMA DEL: $($cutoffDate.ToShortDateString())" -ForegroundColor Yellow Write-Host "--------------------------------------------------" $items = $inbox.Items $items.Sort("[ReceivedTime]", $true) # Ordine cronologico $spostate = 0 Write-Host "Inizio archiviazione..." -ForegroundColor Yellow for ($i = $items.Count; $i -ge 1; $i--) { $item = $items.Item($i) if ($item.Class -eq 43) { # CONFRONTO ESPLICITO if ($item.ReceivedTime -lt $cutoffDate) { $year = $item.ReceivedTime.Year.ToString() $monthName = $item.ReceivedTime.ToString("MM-MMMM") # Cartelle $yearFolder = $null try { $yearFolder = $archiveRoot.Folders.Item($year) } catch { } if ($null -eq $yearFolder) { $yearFolder = $archiveRoot.Folders.Add($year) } $monthFolder = $null try { $monthFolder = $yearFolder.Folders.Item($monthName) } catch { } if ($null -eq $monthFolder) { $monthFolder = $yearFolder.Folders.Add($monthName) } $item.Move($monthFolder) | Out-Null $spostate++ Write-Host "`rProcessate: $spostate (Ultima: $($item.ReceivedTime.ToShortDateString()))" -NoNewline } } [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) | Out-Null } Write-Host "`n`nCompletato! Spostate $spostate email." -ForegroundColor Green } catch { Write-Host "`nErrore: $($_.Exception.Message)" -ForegroundColor Red } finally { if ($outlook) { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null } }