diff --git a/README.md b/README.md index 2f64cf8..53903e7 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,13 @@ schtasks /Create /TN "BakRest\BackupOnLogoff" /XML "tasks\BakRestBackupOnLogoff. schtasks /Create /TN "BakRest\ServerNotifierEvery30Minutes" /XML "tasks\BakRestServerNotifierEvery30Minutes.xml" /F ``` +Se `schtasks` segnala un errore di encoding XML, usa lo script di registrazione che crea una copia temporanea UTF-16 compatibile con Task Scheduler: + +```powershell +powershell -ExecutionPolicy Bypass -File .\scripts\Register-BakRestTask.ps1 -Task Notifier +powershell -ExecutionPolicy Bypass -File .\scripts\Register-BakRestTask.ps1 -Task BackupOnLogoff +``` + Il task di backup usa l'evento Security `4647`, cioe' logoff/disconnessione iniziata dall'utente. Se sulla macchina questo evento non viene scritto, bisogna abilitare l'audit logoff oppure useremo un trigger alternativo. Il task notifier usa un trigger al logon e un trigger giornaliero con ripetizione `PT30M`, quindi avvisa durante la giornata anche se non viene usata la tray app. diff --git a/scripts/Register-BakRestTask.ps1 b/scripts/Register-BakRestTask.ps1 new file mode 100644 index 0000000..3a60097 --- /dev/null +++ b/scripts/Register-BakRestTask.ps1 @@ -0,0 +1,39 @@ +param( + [Parameter(Mandatory = $true)] + [ValidateSet("Notifier", "BackupOnLogoff")] + [string]$Task, + + [string]$TaskFolder = "BakRest" +) + +$ErrorActionPreference = "Stop" + +$repoRoot = Split-Path -Parent $PSScriptRoot + +if ($Task -eq "Notifier") { + $taskName = "$TaskFolder\ServerNotifierEvery30Minutes" + $xmlPath = Join-Path $repoRoot "tasks\BakRestServerNotifierEvery30Minutes.xml" +} else { + $taskName = "$TaskFolder\BackupOnLogoff" + $xmlPath = Join-Path $repoRoot "tasks\BakRestBackupOnLogoff.xml" +} + +if (-not (Test-Path -LiteralPath $xmlPath)) { + throw "Task XML not found: $xmlPath" +} + +$xml = Get-Content -LiteralPath $xmlPath -Raw +$xml = $xml -replace 'encoding="UTF-8"', 'encoding="UTF-16"' + +$tempXml = Join-Path $env:TEMP ("bakrest-task-{0}.xml" -f ([Guid]::NewGuid())) +[System.IO.File]::WriteAllText($tempXml, $xml, [System.Text.Encoding]::Unicode) + +try { + schtasks.exe /Create /TN $taskName /XML $tempXml /F + if ($LASTEXITCODE -ne 0) { + throw "schtasks failed with exit code $LASTEXITCODE" + } + Write-Host "Registered task: $taskName" +} finally { + Remove-Item -LiteralPath $tempXml -Force -ErrorAction SilentlyContinue +}