ESP32/tools/pc-metrics/install-ble-autostart.ps1
2026-06-24 21:01:33 +08:00

52 lines
1.6 KiB
PowerShell

param(
[string]$TaskName = "RLCD Metrics BLE",
[double]$IntervalSeconds = 1,
[int]$StartupDelaySeconds = 20
)
$ErrorActionPreference = "Stop"
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$startScript = Join-Path $scriptDir "start-ble-metrics.ps1"
if (-not (Test-Path $startScript)) {
throw "Cannot find start script: $startScript"
}
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$powershellExe = Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\powershell.exe"
$quotedStartScript = '"' + $startScript + '"'
$actionArgs = "-NoProfile -ExecutionPolicy Bypass -File $quotedStartScript -IntervalSeconds $IntervalSeconds"
$action = New-ScheduledTaskAction -Execute $powershellExe -Argument $actionArgs
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $currentUser
if ($StartupDelaySeconds -gt 0) {
$trigger.Delay = "PT${StartupDelaySeconds}S"
}
$settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-StartWhenAvailable `
-MultipleInstances IgnoreNew
$principal = New-ScheduledTaskPrincipal `
-UserId $currentUser `
-LogonType Interactive `
-RunLevel Limited
Register-ScheduledTask `
-TaskName $TaskName `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-Principal $principal `
-Description "Start the RLCD BLE metrics bridge when the current Windows user logs on." `
-Force | Out-Null
Start-ScheduledTask -TaskName $TaskName
Write-Host "Installed scheduled task: $TaskName"
Write-Host "User: $currentUser"
Write-Host "Start script: $startScript"