[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$PatchScript, [ValidatePattern('^[A-Za-z0-9_-]+$')] [string]$ProviderId = 'openai_http', [string]$TaskName = 'Codex Force HTTP Provider', [switch]$ConfirmedOneTimeValidation ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version 2.0 if (-not $ConfirmedOneTimeValidation) { throw 'Run the patch once, pass strict config_load, and complete one real Codex request before installing the task.' } $PatchScript = [System.IO.Path]::GetFullPath($PatchScript) if (-not (Test-Path -LiteralPath $PatchScript -PathType Leaf)) { throw "Patch script not found: $PatchScript" } $PowerShellExe = (Get-Command powershell.exe -CommandType Application -ErrorAction Stop).Source $QuotedScript = '"{0}"' -f $PatchScript.Replace('"', '""') $Arguments = "-NoLogo -NoProfile -NonInteractive -File $QuotedScript -ProviderId $ProviderId" $Action = New-ScheduledTaskAction -Execute $PowerShellExe -Argument $Arguments $LogonTrigger = New-ScheduledTaskTrigger -AtLogOn -User "$env:USERDOMAIN\$env:USERNAME" $TimerTrigger = New-ScheduledTaskTrigger ` -Once ` -At (Get-Date).AddMinutes(5) ` -RepetitionInterval (New-TimeSpan -Minutes 30) ` -RepetitionDuration (New-TimeSpan -Days 3650) $Principal = New-ScheduledTaskPrincipal ` -UserId "$env:USERDOMAIN\$env:USERNAME" ` -LogonType Interactive ` -RunLevel Limited $Settings = New-ScheduledTaskSettingsSet ` -MultipleInstances IgnoreNew ` -ExecutionTimeLimit (New-TimeSpan -Minutes 10) ` -StartWhenAvailable Register-ScheduledTask ` -TaskName $TaskName ` -Action $Action ` -Trigger @($LogonTrigger, $TimerTrigger) ` -Principal $Principal ` -Settings $Settings ` -Description 'Keeps an already-valid Codex provider on HTTP; no file watcher or resident service.' ` -Force | Out-Null $Task = Get-ScheduledTask -TaskName $TaskName if ($Task.Triggers.Count -ne 2) { Disable-ScheduledTask -TaskName $TaskName | Out-Null throw 'Scheduled task verification failed; task disabled.' } Write-Output "OK: scheduled task registered or updated: $TaskName" Write-Output 'Triggers: current-user logon and every 30 minutes for the configured long-duration window.'