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"