27 lines
1.5 KiB
PowerShell
27 lines
1.5 KiB
PowerShell
$ErrorActionPreference='Stop'
|
|
$target='\\TPS-SVR\TPSBackup'
|
|
|
|
# Pre-flight: confirm we can write to the target as the machine account
|
|
$probe = Join-Path $target ("_probe_{0}.txt" -f $env:COMPUTERNAME)
|
|
try { Set-Content -Path $probe -Value 'ok' -ErrorAction Stop; Remove-Item $probe -Force; Write-Host "target writable: YES" }
|
|
catch { Write-Host "target writable: NO -> $_"; throw }
|
|
|
|
# Detached one-time scheduled task so the backup outlives this RMM command
|
|
$log = 'C:\Windows\Temp\tps-bmr.log'
|
|
$cmd = "wbadmin start backup -backupTarget:$target -allCritical -vssCopy -quiet"
|
|
$action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument "/c $cmd > `"$log`" 2>&1"
|
|
$principal = New-ScheduledTaskPrincipal -UserId 'NT AUTHORITY\SYSTEM' -LogonType ServiceAccount -RunLevel Highest
|
|
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Hours 12)
|
|
$task = New-ScheduledTask -Action $action -Principal $principal -Settings $settings
|
|
Register-ScheduledTask -TaskName 'TPS-BMR-Backup' -InputObject $task -Force | Out-Null
|
|
Start-ScheduledTask -TaskName 'TPS-BMR-Backup'
|
|
Start-Sleep -Seconds 12
|
|
|
|
"==== TASK STATE ===="
|
|
Get-ScheduledTask -TaskName 'TPS-BMR-Backup' | Select-Object TaskName,State | Format-Table -Auto | Out-String
|
|
"==== WBADMIN STATUS ===="
|
|
wbadmin get status 2>&1 | Out-String
|
|
"==== LOG HEAD ===="
|
|
if(Test-Path $log){ Get-Content $log -TotalCount 15 | Out-String } else { "(log not created yet)" }
|
|
"==== DONE ===="
|