vignette/scripts/install-public-runtime-task.ps1
2026-06-27 16:08:41 +09:00

88 lines
2.7 KiB
PowerShell

param(
[string]$Workspace = "D:\workspace\vignette",
[string]$TaskName = "VignettePublicRuntimeWatchdog",
[int]$IntervalMinutes = 5,
[string[]]$AdditionalPublicHealthUrls = @(),
[switch]$SkipPublicHealth,
[switch]$SkipCloudflaredRestart,
[switch]$RunNow
)
$ErrorActionPreference = "Stop"
if ($IntervalMinutes -lt 1) {
throw "IntervalMinutes must be 1 or greater"
}
$watchScript = Join-Path $Workspace "scripts\watch-public-runtime.ps1"
if (!(Test-Path $watchScript)) {
throw "Watchdog script not found at $watchScript"
}
$powershell = (Get-Command powershell.exe).Source
$userId = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$actionArguments = @(
"-NoProfile",
"-ExecutionPolicy Bypass",
"-File `"$watchScript`"",
"-Workspace `"$Workspace`""
)
if ($SkipPublicHealth) {
$actionArguments += "-SkipPublicHealth"
}
if ($SkipCloudflaredRestart) {
$actionArguments += "-SkipCloudflaredRestart"
}
if ($AdditionalPublicHealthUrls.Count -gt 0) {
$escapedUrls = $AdditionalPublicHealthUrls | ForEach-Object { "`"$_`"" }
$actionArguments += "-AdditionalPublicHealthUrls $($escapedUrls -join ',')"
}
$action = New-ScheduledTaskAction `
-Execute $powershell `
-Argument ($actionArguments -join " ") `
-WorkingDirectory $Workspace
$logonTrigger = New-ScheduledTaskTrigger -AtLogOn -User $userId
$repeatTrigger = New-ScheduledTaskTrigger `
-Once `
-At (Get-Date).AddMinutes(1) `
-RepetitionInterval (New-TimeSpan -Minutes $IntervalMinutes)
$settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-ExecutionTimeLimit (New-TimeSpan -Minutes 10) `
-MultipleInstances IgnoreNew `
-RestartCount 3 `
-RestartInterval (New-TimeSpan -Minutes 1) `
-StartWhenAvailable `
-WakeToRun
$principal = New-ScheduledTaskPrincipal `
-UserId $userId `
-LogonType Interactive `
-RunLevel Limited
$description = "Runs Vignette public runtime watchdog as $userId. Secrets stay in the user profile and apps/api/.env; the task command stores no secrets."
$task = New-ScheduledTask `
-Action $action `
-Trigger @($logonTrigger, $repeatTrigger) `
-Settings $settings `
-Principal $principal `
-Description $description
Register-ScheduledTask -TaskName $TaskName -InputObject $task -Force | Out-Null
Write-Output "Installed scheduled task '$TaskName' for $userId"
Write-Output "Action: $powershell $($actionArguments -join ' ')"
Write-Output "Interval: every $IntervalMinutes minute(s), plus at user logon"
if ($AdditionalPublicHealthUrls.Count -gt 0) {
Write-Output "Additional public health URLs: $($AdditionalPublicHealthUrls -join ', ')"
}
if ($RunNow) {
Start-ScheduledTask -TaskName $TaskName
Write-Output "Started scheduled task '$TaskName'"
}