Karanténa 14 dní / Microsoft 365
# ========== UPRAVIT ==========
$user = "hledanyuzivatel@domena.cz"
$outFolder = "$env:USERPROFILE\Desktop\QuarantineExport_$($user.Replace('@','_'))"
# ==============================
# 1) Nainstalovat modul pokud chybí (nebo odkomentovat pokud nechceš instalovat automaticky)
if (-not (Get-Module -ListAvailable -Name ExchangeOnlineManagement)) {
Write-Host "Instaluju ExchangeOnlineManagement modul..." -ForegroundColor Yellow
Install-Module ExchangeOnlineManagement -Scope CurrentUser -Force -AllowClobber
}
Import-Module ExchangeOnlineManagement -ErrorAction Stop
# 2) Připojit se k Exchange Online (zde upravte účet - userprincipalname)
Write-Host "Připojuji k Exchange Online... (přihlašovací okno)" -ForegroundColor Cyan
Connect-ExchangeOnline -UserPrincipalName ucetsopravnenim@domena.cz -ShowProgress $true
# 3) Vytvořit složku výstupu
if (-not (Test-Path -Path $outFolder)) { New-Item -Path $outFolder -ItemType Directory | Out-Null }
# 4) Získat položky v karanténě pro uživatele
Write-Host "Načítám položky v karanténě pro $user ..." -ForegroundColor Cyan
$cutoffDate = (Get-Date).AddDays(-14)
try {
$items = Get-QuarantineMessage -RecipientAddress $user -ErrorAction Stop | Where-Object { $_.ReceivedTime -ge $cutoffDate }
} catch {
Write-Warning "Get-QuarantineMessage failed: pokusím se alternativně přes Get-QuarantineMessage (možná chybí práva). Pokračuji s prázdným výsledkem."
$items = @()
}
if ($items.Count -eq 0) {
Write-Warning "Nebyly nalezeny žádné položky v karanténě pro $user (nebo nemáš práva / cmdlet není dostupný)."
} else {
# 5) Vybrat smysluplné vlastnosti (opravený Sender)
$export = $items | Select-Object @{n='ReceivedTime';e={($_.ReceivedTime)}},
@{n='Subject';e={($_.Subject)}},
@{n='Sender';e={
if ($_.From -and $_.From.Address) { $_.From.Address }
elseif ($_.FromAddress) { $_.FromAddress }
elseif ($_.SenderAddress) { $_.SenderAddress }
else { "Unknown" }
}},
@{n='Recipient';e={($_.Recipients -join '; ')}},
@{n='QuarantineType';e={($_.QuarantineType)}},
@{n='Reason';e={($_.DetectionReason) -or ($_.PolicyTip) -or ($_.Reason)}},
@{n='MessageId';e={($_.MessageId)}},
@{n='SizeBytes';e={($_.Size) -as [int]}}
# 6) Export CSV
$csvPath = Join-Path $outFolder "quarantine_${($user.Split('@')[0])}.csv"
$export | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8
Write-Host "Export do CSV uložen: $csvPath" -ForegroundColor Green
# 7) Vytvořit jednoduché HTML (přehledné)
$htmlPath = Join-Path $outFolder "quarantine_${($user.Split('@')[0])}.html"
$htmlHeader = "<h2>Quarantine report for $user - generated $(Get-Date -Format 'yyyy-MM-dd HH:mm')</h2>"
$htmlTable = $export | ConvertTo-Html -Property ReceivedTime,Subject,Sender,Recipient,QuarantineType,Reason,MessageId,SizeBytes -PreContent $htmlHeader -Fragment
$htmlFull = "<html><head><meta charset='utf-8'><title>Quarantine report - $user</title></head><body>$htmlTable</body></html>"
$htmlFull | Out-File -FilePath $htmlPath -Encoding UTF8
Write-Host "HTML report uložen: $htmlPath" -ForegroundColor Green
# 8) Otevřít HTML v prohlížeči
Invoke-Item $htmlPath
}
# 9) Odpojit session (dobré praktiky)
Disconnect-ExchangeOnline -Confirm:$false
Write-Host "Hotovo." -ForegroundColor Cyan
Designo