83 lines
2.8 KiB
PowerShell
83 lines
2.8 KiB
PowerShell
$ErrorActionPreference = "Stop"
|
|
|
|
$ProjectDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
$TaskName = "DocuTranslate"
|
|
$WatchdogTaskName = "DocuTranslateWatchdog"
|
|
$RunnerBat = Join-Path $ProjectDir "autostart_runner.bat"
|
|
$WatchdogBat = Join-Path $ProjectDir "watchdog.bat"
|
|
$LogDir = Join-Path $ProjectDir "logs"
|
|
$LogFile = Join-Path $LogDir "autostart.log"
|
|
|
|
if (!(Test-Path -LiteralPath $RunnerBat)) {
|
|
throw "Cannot find $RunnerBat"
|
|
}
|
|
|
|
if (!(Test-Path -LiteralPath $WatchdogBat)) {
|
|
throw "Cannot find $WatchdogBat"
|
|
}
|
|
|
|
if (!(Test-Path -LiteralPath $LogDir)) {
|
|
New-Item -ItemType Directory -Path $LogDir | Out-Null
|
|
}
|
|
|
|
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest
|
|
$StartupAction = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c `"$RunnerBat`""
|
|
$StartupTrigger = New-ScheduledTaskTrigger -AtStartup
|
|
try {
|
|
$StartupSettings = New-ScheduledTaskSettingsSet `
|
|
-StartWhenAvailable `
|
|
-MultipleInstances IgnoreNew `
|
|
-ExecutionTimeLimit ([TimeSpan]::Zero) `
|
|
-DontStopIfGoingOnBatteries `
|
|
-RestartCount 3 `
|
|
-RestartInterval (New-TimeSpan -Minutes 1)
|
|
} catch {
|
|
$StartupSettings = New-ScheduledTaskSettingsSet `
|
|
-StartWhenAvailable `
|
|
-MultipleInstances IgnoreNew `
|
|
-ExecutionTimeLimit ([TimeSpan]::Zero) `
|
|
-DontStopIfGoingOnBatteries
|
|
}
|
|
|
|
Register-ScheduledTask `
|
|
-TaskName $TaskName `
|
|
-Action $StartupAction `
|
|
-Trigger $StartupTrigger `
|
|
-Principal $Principal `
|
|
-Settings $StartupSettings `
|
|
-Force | Out-Null
|
|
|
|
$WatchdogAction = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c `"$WatchdogBat`""
|
|
$WatchdogTrigger = New-ScheduledTaskTrigger `
|
|
-Once `
|
|
-At (Get-Date).AddMinutes(1) `
|
|
-RepetitionInterval (New-TimeSpan -Minutes 5) `
|
|
-RepetitionDuration (New-TimeSpan -Days 3650)
|
|
$WatchdogSettings = New-ScheduledTaskSettingsSet `
|
|
-StartWhenAvailable `
|
|
-MultipleInstances IgnoreNew `
|
|
-ExecutionTimeLimit (New-TimeSpan -Minutes 2) `
|
|
-DontStopIfGoingOnBatteries
|
|
|
|
Register-ScheduledTask `
|
|
-TaskName $WatchdogTaskName `
|
|
-Action $WatchdogAction `
|
|
-Trigger $WatchdogTrigger `
|
|
-Principal $Principal `
|
|
-Settings $WatchdogSettings `
|
|
-Force | Out-Null
|
|
|
|
Write-Host "[OK] Scheduled task `"$TaskName`" has been created."
|
|
Write-Host "[OK] Scheduled task `"$WatchdogTaskName`" has been created."
|
|
Write-Host "[OK] Startup runner: `"$RunnerBat`""
|
|
Write-Host "[OK] Watchdog: `"$WatchdogBat`""
|
|
Write-Host "[OK] Log file: `"$LogFile`""
|
|
Write-Host "[OK] Startup task execution limit: disabled"
|
|
Write-Host ""
|
|
Write-Host "Verify:"
|
|
Write-Host "schtasks /Query /TN `"$TaskName`" /V /FO LIST"
|
|
Write-Host "schtasks /Query /TN `"$WatchdogTaskName`" /V /FO LIST"
|
|
Write-Host ""
|
|
Write-Host "Start now:"
|
|
Write-Host "schtasks /Run /TN `"$TaskName`""
|