Supporta task backup con credenziali memorizzate
This commit is contained in:
@@ -7,6 +7,10 @@ param(
|
|||||||
|
|
||||||
[switch]$RunAsCurrentUser,
|
[switch]$RunAsCurrentUser,
|
||||||
|
|
||||||
|
[switch]$RunAsStoredCredentials,
|
||||||
|
|
||||||
|
[string]$UserName,
|
||||||
|
|
||||||
[switch]$NoShutdown
|
[switch]$NoShutdown
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -45,6 +49,10 @@ 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>'
|
$xml = $xml -replace '<Arguments>-m bakrest\.tray_app --nogui</Arguments>', '<Arguments>-m bakrest.tray_app --nogui --no-shutdown</Arguments>'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($RunAsStoredCredentials -and $RunAsCurrentUser) {
|
||||||
|
throw "Use either -RunAsCurrentUser or -RunAsStoredCredentials, not both."
|
||||||
|
}
|
||||||
|
|
||||||
if ($RunAsCurrentUser) {
|
if ($RunAsCurrentUser) {
|
||||||
$currentIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
$currentIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
||||||
$currentSid = $currentIdentity.User.Value
|
$currentSid = $currentIdentity.User.Value
|
||||||
@@ -65,11 +73,48 @@ if ($RunAsCurrentUser) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$plainPassword = $null
|
||||||
|
$runAsName = $null
|
||||||
|
if ($RunAsStoredCredentials) {
|
||||||
|
if (-not $UserName) {
|
||||||
|
$UserName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
|
||||||
|
}
|
||||||
|
|
||||||
|
$securePassword = Read-Host -Prompt "Password for $UserName" -AsSecureString
|
||||||
|
$passwordPtr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
|
||||||
|
try {
|
||||||
|
$plainPassword = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($passwordPtr)
|
||||||
|
} finally {
|
||||||
|
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($passwordPtr)
|
||||||
|
}
|
||||||
|
|
||||||
|
$principalXml = @"
|
||||||
|
<Principals>
|
||||||
|
<Principal id="Author">
|
||||||
|
<UserId>$([System.Security.SecurityElement]::Escape($UserName))</UserId>
|
||||||
|
<LogonType>Password</LogonType>
|
||||||
|
<RunLevel>LeastPrivilege</RunLevel>
|
||||||
|
</Principal>
|
||||||
|
</Principals>
|
||||||
|
"@
|
||||||
|
$xml = [regex]::Replace(
|
||||||
|
$xml,
|
||||||
|
'<Principals>.*?</Principals>',
|
||||||
|
$principalXml,
|
||||||
|
[System.Text.RegularExpressions.RegexOptions]::Singleline
|
||||||
|
)
|
||||||
|
$runAsName = $UserName
|
||||||
|
}
|
||||||
|
|
||||||
$tempXml = Join-Path $env:TEMP ("bakrest-task-{0}.xml" -f ([Guid]::NewGuid()))
|
$tempXml = Join-Path $env:TEMP ("bakrest-task-{0}.xml" -f ([Guid]::NewGuid()))
|
||||||
[System.IO.File]::WriteAllText($tempXml, $xml, [System.Text.Encoding]::Unicode)
|
[System.IO.File]::WriteAllText($tempXml, $xml, [System.Text.Encoding]::Unicode)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if ($RunAsStoredCredentials) {
|
||||||
|
schtasks.exe /Create /TN $taskName /XML $tempXml /RU $UserName /RP $plainPassword /F
|
||||||
|
} else {
|
||||||
schtasks.exe /Create /TN $taskName /XML $tempXml /F
|
schtasks.exe /Create /TN $taskName /XML $tempXml /F
|
||||||
|
}
|
||||||
if ($LASTEXITCODE -ne 0) {
|
if ($LASTEXITCODE -ne 0) {
|
||||||
throw "schtasks failed with exit code $LASTEXITCODE"
|
throw "schtasks failed with exit code $LASTEXITCODE"
|
||||||
}
|
}
|
||||||
@@ -79,9 +124,13 @@ try {
|
|||||||
if ($RunAsCurrentUser) {
|
if ($RunAsCurrentUser) {
|
||||||
Write-Host "RunAs: $($currentIdentity.Name)"
|
Write-Host "RunAs: $($currentIdentity.Name)"
|
||||||
}
|
}
|
||||||
|
if ($RunAsStoredCredentials) {
|
||||||
|
Write-Host "RunAs: $runAsName (stored credentials)"
|
||||||
|
}
|
||||||
if ($NoShutdown -and $Task -eq "BackupOnLogoff") {
|
if ($NoShutdown -and $Task -eq "BackupOnLogoff") {
|
||||||
Write-Host "BackupOnLogoff registered in debug mode: --no-shutdown"
|
Write-Host "BackupOnLogoff registered in debug mode: --no-shutdown"
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
$plainPassword = $null
|
||||||
Remove-Item -LiteralPath $tempXml -Force -ErrorAction SilentlyContinue
|
Remove-Item -LiteralPath $tempXml -Force -ErrorAction SilentlyContinue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user