senza gui

This commit is contained in:
Alessandro Battilani
2026-01-10 15:36:02 +01:00
parent 2d9426e87f
commit 9cf6040a59
6 changed files with 1089 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
# --- CONFIGURAZIONE ---
$archiveName = "Archivio online - alessandro.battilani@intesasanpaolo.com"
$monthsLimit = -6
$monthsLimit = -8
# ----------------------
try {
@@ -15,9 +15,14 @@ try {
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]", $false)
$items.Sort("[ReceivedTime]", $true) # Ordine cronologico
$spostate = 0
Write-Host "Inizio archiviazione..." -ForegroundColor Yellow
@@ -25,13 +30,14 @@ try {
for ($i = $items.Count; $i -ge 1; $i--) {
$item = $items.Item($i)
if ($item.Class -eq 43) { # olMail
if ($item.Class -eq 43) {
# CONFRONTO ESPLICITO
if ($item.ReceivedTime -lt $cutoffDate) {
$year = $item.ReceivedTime.Year.ToString()
$monthName = $item.ReceivedTime.ToString("MM-MMMM")
# Navigazione/Creazione cartelle
# Cartelle
$yearFolder = $null
try { $yearFolder = $archiveRoot.Folders.Item($year) } catch { }
if ($null -eq $yearFolder) { $yearFolder = $archiveRoot.Folders.Add($year) }
@@ -40,27 +46,19 @@ try {
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
Write-Host "`rProcessate: $spostate (Ultima: $($item.ReceivedTime.ToShortDateString()))" -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
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
}
if ($outlook) { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null }
}