$ErrorActionPreference = "Stop" [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [System.Text.Encoding]::UTF8 chcp 65001 | Out-Null $Project = Split-Path -Parent $PSScriptRoot $TaskPath = "\StockBot\" $Weekdays = @( [DayOfWeek]::Monday, [DayOfWeek]::Tuesday, [DayOfWeek]::Wednesday, [DayOfWeek]::Thursday, [DayOfWeek]::Friday ) function Register-StockTask { param( [string]$Name, [string]$Time, [string]$Script, [int]$LimitMinutes ) $Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $Weekdays -At $Time $Action = New-ScheduledTaskAction ` -Execute "powershell.exe" ` -Argument "-NonInteractive -ExecutionPolicy Bypass -File `"$Project\scripts\$Script`"" ` -WorkingDirectory $Project $Settings = New-ScheduledTaskSettingsSet ` -ExecutionTimeLimit (New-TimeSpan -Minutes $LimitMinutes) ` -StartWhenAvailable ` -WakeToRun ` -DontStopIfGoingOnBatteries ` -RunOnlyIfNetworkAvailable:$false $Settings.DisallowStartIfOnBatteries = $false Register-ScheduledTask ` -TaskName $Name ` -TaskPath $TaskPath ` -Trigger $Trigger ` -Action $Action ` -Settings $Settings ` -RunLevel Limited ` -Force | Out-Null Write-Host "[OK] $Name registered at $Time" -ForegroundColor Green } function Register-WatchdogTask { $ScriptPath = Join-Path $Project "scripts\run_watchdog.ps1" $Command = 'schtasks /Create /TN "\StockBot\StockBot_Watchdog" /TR "\"powershell.exe\" -NonInteractive -ExecutionPolicy Bypass -File \"' + $ScriptPath + '\"" /SC WEEKLY /D MON,TUE,WED,THU,FRI /ST 09:00 /RI 5 /DU 06:05 /F' cmd.exe /c $Command | Out-Null if ($LASTEXITCODE -ne 0) { throw "StockBot_Watchdog registration failed" } $Task = Get-ScheduledTask -TaskName "StockBot_Watchdog" -TaskPath $TaskPath $Task.Settings.StartWhenAvailable = $true $Task.Settings.WakeToRun = $true $Task.Settings.DisallowStartIfOnBatteries = $false $Task.Settings.StopIfGoingOnBatteries = $false Set-ScheduledTask -TaskName "StockBot_Watchdog" -TaskPath $TaskPath -Settings $Task.Settings | Out-Null Write-Host "[OK] StockBot_Watchdog registered weekdays at 09:00-15:05 every 5 minutes" -ForegroundColor Green } Register-StockTask "StockBot_Morning" "08:15" "run_morning.ps1" 20 Register-StockTask "StockBot_Midday" "11:20" "run_midday.ps1" 20 Register-StockTask "StockBot_Evening" "15:30" "run_evening.ps1" 30 Register-StockTask "StockBot_Training" "16:00" "run_training_pipeline.ps1" 60 Register-WatchdogTask $BotTask = Get-ScheduledTask -TaskName "StockBot_Bot" -TaskPath $TaskPath -ErrorAction SilentlyContinue if ($BotTask) { Disable-ScheduledTask -TaskName "StockBot_Bot" -TaskPath $TaskPath | Out-Null Write-Host "[OK] StockBot_Bot disabled" -ForegroundColor Yellow } Write-Host "" Write-Host "Registered StockBot tasks:" -ForegroundColor Cyan Get-ScheduledTask -TaskPath $TaskPath | Format-Table TaskName, State