66 lines
2.3 KiB
PowerShell
66 lines
2.3 KiB
PowerShell
# --- CONFIGURAZIONE ---
|
|
$archiveName = "Archivio online - alessandro.battilani@intesasanpaolo.com"
|
|
$monthsLimit = -6
|
|
# ----------------------
|
|
|
|
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
|
|
}
|
|
|
|
$cutoffDate = (Get-Date).AddMonths($monthsLimit)
|
|
$items = $inbox.Items
|
|
$items.Sort("[ReceivedTime]", $false)
|
|
|
|
$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) { # olMail
|
|
if ($item.ReceivedTime -lt $cutoffDate) {
|
|
|
|
$year = $item.ReceivedTime.Year.ToString()
|
|
$monthName = $item.ReceivedTime.ToString("MM-MMMM")
|
|
|
|
# Navigazione/Creazione 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) }
|
|
|
|
# ESECUZIONE SPOSTAMENTO
|
|
# Il trucco è aggiungere "| Out-Null" per evitare che PowerShell stampi l'oggetto risultante
|
|
$item.Move($monthFolder) | Out-Null
|
|
|
|
$spostate++
|
|
|
|
# Feedback minimale: solo un contatore sulla stessa riga
|
|
Write-Host "`rEmail archiviate: $spostate" -NoNewline
|
|
}
|
|
}
|
|
# Importante: rilasciare il riferimento all'oggetto singolo per velocizzare
|
|
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) | Out-Null
|
|
}
|
|
|
|
Write-Host "`n`nCompletato! Totale spostate: $spostate" -ForegroundColor Green
|
|
}
|
|
catch {
|
|
Write-Host "`nErrore: $($_.Exception.Message)" -ForegroundColor Red
|
|
}
|
|
finally {
|
|
if ($outlook) {
|
|
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
|
|
}
|
|
} |