99 lines
4.2 KiB
PowerShell
99 lines
4.2 KiB
PowerShell
# --- CONFIGURAZIONE ---
|
|
$pstPath = "C:\Percorso\Tuo\File\archivio_vecchio.pst"
|
|
$archiveName = "IL NOME DEL TUO ARCHIVIO ONLINE"
|
|
$logFile = ".\Report_Migrazione_Totale.txt"
|
|
# ----------------------
|
|
|
|
# Inizializza Log e Memoria Duplicati
|
|
"REPORT MIGRAZIONE TOTALE PST - $(Get-Date)" | Out-File $logFile
|
|
"------------------------------------------" | Out-File $logFile -Append
|
|
$script:duplicatiSaltati = 0
|
|
$script:spostateTotali = 0
|
|
$script:mappaEmailEsistenti = @{}
|
|
|
|
# Rinominata da Build-ExistMap a Initialize-ExistMap (Verbo approvato)
|
|
function Initialize-ExistMap($folder) {
|
|
foreach ($item in $folder.Items) {
|
|
if ($item.Class -eq 43) {
|
|
$key = "$($item.Subject)|$($item.ReceivedTime.Ticks)|$($item.SenderEmailAddress)"
|
|
$script:mappaEmailEsistenti[$key] = $true
|
|
}
|
|
}
|
|
foreach ($sub in $folder.Folders) { Initialize-ExistMap $sub }
|
|
}
|
|
|
|
# Rinominata da Process-Folder a Invoke-FolderMigration (Verbo approvato)
|
|
function Invoke-FolderMigration($outlookFolder, $archiveRoot) {
|
|
$items = $outlookFolder.Items
|
|
$countInFolder = 0
|
|
|
|
if ($items.Count -gt 0) {
|
|
Write-Host "`nAnalisi cartella: $($outlookFolder.FolderPath)" -ForegroundColor Cyan
|
|
|
|
for ($i = $items.Count; $i -ge 1; $i--) {
|
|
$item = $items.Item($i)
|
|
if ($item.Class -eq 43) {
|
|
$key = "$($item.Subject)|$($item.ReceivedTime.Ticks)|$($item.SenderEmailAddress)"
|
|
|
|
if ($script:mappaEmailEsistenti.ContainsKey($key)) {
|
|
$script:duplicatiSaltati++
|
|
continue
|
|
}
|
|
|
|
$year = $item.ReceivedTime.Year.ToString()
|
|
$monthName = $item.ReceivedTime.ToString("MM-MMMM")
|
|
|
|
$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
|
|
$script:spostateTotali++
|
|
$countInFolder++
|
|
Write-Host "`rSpostate: $script:spostateTotali | Duplicati: $script:duplicatiSaltati" -NoNewline
|
|
}
|
|
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) | Out-Null
|
|
}
|
|
}
|
|
|
|
if ($countInFolder -gt 0) {
|
|
"Spostate $countInFolder email da: $($outlookFolder.FolderPath)" | Out-File $script:logFile -Append
|
|
}
|
|
foreach ($subFolder in $outlookFolder.Folders) { Invoke-FolderMigration $subFolder $archiveRoot }
|
|
}
|
|
|
|
try {
|
|
$outlook = New-Object -ComObject Outlook.Application
|
|
$namespace = $outlook.GetNamespace("MAPI")
|
|
|
|
$archiveRoot = $namespace.Folders.Item($archiveName)
|
|
if ($null -eq $archiveRoot) { throw "Archivio Online '$archiveName' non trovato." }
|
|
|
|
Write-Host "Mappatura email esistenti nell'archivio cloud..." -ForegroundColor Yellow
|
|
Initialize-ExistMap $archiveRoot
|
|
Write-Host "Mappatura completata. Email trovate: $($script:mappaEmailEsistenti.Count)"
|
|
|
|
Write-Host "`nCaricamento PST: $pstPath" -ForegroundColor Yellow
|
|
$namespace.AddStore($pstPath)
|
|
$pstStore = $namespace.Stores | Where-Object { $_.FilePath -eq $pstPath }
|
|
$pstRoot = $pstStore.GetRootFolder()
|
|
|
|
Invoke-FolderMigration $pstRoot $archiveRoot
|
|
|
|
Write-Host "`n`nMigrazione terminata con successo!" -ForegroundColor Green
|
|
"------------------------------------------" | Out-File $logFile -Append
|
|
"TOTALE SPOSTATE: $script:spostateTotali" | Out-File $logFile -Append
|
|
"DUPLICATI SALTATI: $script:duplicatiSaltati" | Out-File $logFile -Append
|
|
}
|
|
catch {
|
|
Write-Host "`nErrore: $($_.Exception.Message)" -ForegroundColor Red
|
|
"ERRORE: $($_.Exception.Message)" | Out-File $logFile -Append
|
|
}
|
|
finally {
|
|
if ($pstRoot) { $namespace.RemoveStore($pstRoot) }
|
|
if ($outlook) { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null }
|
|
} |