From 976e53a193f0a342c36529c4bc5252ceb23ccbd1 Mon Sep 17 00:00:00 2001 From: allebonvi Date: Thu, 2 Jul 2026 12:30:49 +0200 Subject: [PATCH] Registra task su utente corrente --- README.md | 12 +++++++++++ scripts/Register-BakRestTask.ps1 | 36 +++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 53903e7..fe3c246 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,18 @@ powershell -ExecutionPolicy Bypass -File .\scripts\Register-BakRestTask.ps1 -Tas powershell -ExecutionPolicy Bypass -File .\scripts\Register-BakRestTask.ps1 -Task BackupOnLogoff ``` +Per registrare il task di backup sull'utente corrente invece che sul gruppo `Users`: + +```powershell +powershell -ExecutionPolicy Bypass -File .\scripts\Register-BakRestTask.ps1 -Task BackupOnLogoff -RunAsCurrentUser +``` + +Per debug senza spegnimento: + +```powershell +powershell -ExecutionPolicy Bypass -File .\scripts\Register-BakRestTask.ps1 -Task BackupOnLogoff -RunAsCurrentUser -NoShutdown +``` + 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 index e61a923..a196af0 100644 --- a/scripts/Register-BakRestTask.ps1 +++ b/scripts/Register-BakRestTask.ps1 @@ -3,7 +3,11 @@ param( [ValidateSet("Notifier", "BackupOnLogoff")] [string]$Task, - [string]$TaskFolder = "BakRest" + [string]$TaskFolder = "BakRest", + + [switch]$RunAsCurrentUser, + + [switch]$NoShutdown ) $ErrorActionPreference = "Stop" @@ -37,6 +41,30 @@ $xml = $xml -replace 'encoding="UTF-8"', 'encoding="UTF-16"' $xml = $xml -replace '.*?', ('{0}' -f [System.Security.SecurityElement]::Escape($pythonExe)) $xml = $xml -replace '.*?', ('{0}' -f [System.Security.SecurityElement]::Escape($repoRoot)) +if ($NoShutdown -and $Task -eq "BackupOnLogoff") { + $xml = $xml -replace '-m bakrest\.tray_app --nogui', '-m bakrest.tray_app --nogui --no-shutdown' +} + +if ($RunAsCurrentUser) { + $currentIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() + $currentSid = $currentIdentity.User.Value + $principalXml = @" + + + $currentSid + InteractiveToken + LeastPrivilege + + +"@ + $xml = [regex]::Replace( + $xml, + '.*?', + $principalXml, + [System.Text.RegularExpressions.RegexOptions]::Singleline + ) +} + $tempXml = Join-Path $env:TEMP ("bakrest-task-{0}.xml" -f ([Guid]::NewGuid())) [System.IO.File]::WriteAllText($tempXml, $xml, [System.Text.Encoding]::Unicode) @@ -48,6 +76,12 @@ try { Write-Host "Registered task: $taskName" Write-Host "Python: $pythonExe" Write-Host "WorkingDirectory: $repoRoot" + if ($RunAsCurrentUser) { + Write-Host "RunAs: $($currentIdentity.Name)" + } + if ($NoShutdown -and $Task -eq "BackupOnLogoff") { + Write-Host "BackupOnLogoff registered in debug mode: --no-shutdown" + } } finally { Remove-Item -LiteralPath $tempXml -Force -ErrorAction SilentlyContinue }