xyc
4 天以前 922823a3f1faaf0293256e3ffb1285eaeb8a3b23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<#
  Register / remove / inspect a Windows Scheduled Task that starts the TrafficAudit
  backend at machine boot, with no user session required.
 
  Run from an ELEVATED PowerShell in the backend folder (jar + application.yml +
  this script + start-backend-service.cmd):
 
    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1
    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -DelaySeconds 90
    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -StartNow
    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -Status
    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -Remove
 
  What it does:
    - trigger  : AtStartup, delayed by -DelaySeconds (default 60s, so MySQL is up first)
    - principal: SYSTEM, highest privileges, "run whether user is logged on or not"
    - settings : no execution time limit, start when available, restart 3x / 1 min on failure
    - action   : cmd.exe /c start-backend-service.cmd (logs to app.log next to the jar)
 
  Notes:
    - Re-running without -Remove is idempotent (-Force overwrites the task).
    - Start/stop manually with: Start-ScheduledTask / Stop-ScheduledTask -TaskName <name>
    - Keep this file ASCII-only: Windows PowerShell 5.1 reads a BOM-less .ps1 as ANSI.
#>
param(
  [string]$TaskName = 'TrafficAuditBackend',
  [int]$DelaySeconds = 60,
  [switch]$Remove,
  [switch]$Status,
  [switch]$StartNow
)
 
$ErrorActionPreference = 'Stop'
$root     = Split-Path -Parent $MyInvocation.MyCommand.Path
$launcher = Join-Path $root 'start-backend-service.cmd'
$logFile  = Join-Path $root 'app.log'
 
function Assert-Admin {
  $identity  = [Security.Principal.WindowsIdentity]::GetCurrent()
  $principal = New-Object Security.Principal.WindowsPrincipal($identity)
  if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    throw 'This script must run from an elevated (Run as Administrator) PowerShell.'
  }
}
 
if ($Status) {
  $task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
  if (-not $task) { Write-Host "Scheduled task '$TaskName' is not registered."; exit 1 }
  $task | Select-Object TaskName, TaskPath, State | Format-List
  Get-ScheduledTaskInfo -TaskName $TaskName |
    Select-Object LastRunTime, LastTaskResult, NextRunTime, NumberOfMissedRuns | Format-List
  exit 0
}
 
if ($Remove) {
  Assert-Admin
  if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) {
    Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
    Write-Host "Removed scheduled task '$TaskName'."
  } else {
    Write-Host "Scheduled task '$TaskName' was not registered; nothing to remove."
  }
  exit 0
}
 
Assert-Admin
if (-not (Test-Path $launcher)) { throw "Missing launcher: $launcher" }
if (-not (Test-Path (Join-Path $root 'traffic-audit-server-1.0.0-SNAPSHOT.jar'))) {
  Write-Host "WARN: jar not found in $root - the task will be registered, but startup will fail until the jar is there."
}
 
$action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument ('/c "' + $launcher + '"') -WorkingDirectory $root
 
$trigger = New-ScheduledTaskTrigger -AtStartup
if ($DelaySeconds -gt 0) { $trigger.Delay = 'PT' + $DelaySeconds + 'S' }
 
$principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest
 
$settings = New-ScheduledTaskSettingsSet `
  -AllowStartIfOnBatteries `
  -DontStopIfGoingOnBatteries `
  -StartWhenAvailable `
  -MultipleInstances IgnoreNew `
  -RestartCount 3 `
  -RestartInterval (New-TimeSpan -Minutes 1) `
  -ExecutionTimeLimit ([TimeSpan]::Zero)
 
Register-ScheduledTask `
  -TaskName    $TaskName `
  -Action      $action `
  -Trigger     $trigger `
  -Principal   $principal `
  -Settings    $settings `
  -Description 'TrafficAudit backend auto-start at machine boot (script-managed).' `
  -Force | Out-Null
 
Write-Host "Registered scheduled task '$TaskName'."
Write-Host "  Trigger  : At startup, +${DelaySeconds}s"
Write-Host "  Principal: SYSTEM (highest privileges, no logon required)"
Write-Host "  Launcher : $launcher"
Write-Host "  Log      : $logFile"
Write-Host ''
Write-Host "Service address after boot: http://<server-ip>:8090"
Write-Host "Manage it with: -Status | -StartNow | -Remove"
 
if ($StartNow) {
  Start-ScheduledTask -TaskName $TaskName
  Write-Host "Started task '$TaskName' now."
}