Registra task su utente corrente
This commit is contained in:
@@ -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 '<Command>.*?</Command>', ('<Command>{0}</Command>' -f [System.Security.SecurityElement]::Escape($pythonExe))
|
||||
$xml = $xml -replace '<WorkingDirectory>.*?</WorkingDirectory>', ('<WorkingDirectory>{0}</WorkingDirectory>' -f [System.Security.SecurityElement]::Escape($repoRoot))
|
||||
|
||||
if ($NoShutdown -and $Task -eq "BackupOnLogoff") {
|
||||
$xml = $xml -replace '<Arguments>-m bakrest\.tray_app --nogui</Arguments>', '<Arguments>-m bakrest.tray_app --nogui --no-shutdown</Arguments>'
|
||||
}
|
||||
|
||||
if ($RunAsCurrentUser) {
|
||||
$currentIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
||||
$currentSid = $currentIdentity.User.Value
|
||||
$principalXml = @"
|
||||
<Principals>
|
||||
<Principal id="Author">
|
||||
<UserId>$currentSid</UserId>
|
||||
<LogonType>InteractiveToken</LogonType>
|
||||
<RunLevel>LeastPrivilege</RunLevel>
|
||||
</Principal>
|
||||
</Principals>
|
||||
"@
|
||||
$xml = [regex]::Replace(
|
||||
$xml,
|
||||
'<Principals>.*?</Principals>',
|
||||
$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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user