-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-task.ps1
More file actions
71 lines (61 loc) · 2.37 KB
/
Copy pathsetup-task.ps1
File metadata and controls
71 lines (61 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# ============================================================
# Claude Code 5-hour window trigger - Task Scheduler setup
# Run this ONCE in PowerShell to register the daily 9:00 AM task
# ============================================================
param(
[string]$TaskName = "ClaudeCode-Window-Trigger-9AM",
[string]$TriggerTime = "9:00AM"
)
$scriptDir = $PSScriptRoot
$scriptPath = Join-Path $scriptDir "trigger-claude.bat"
$description = "Sends 'hi' to Claude Haiku at $TriggerTime to start the 5-hour usage window; missed runs are not started later"
# Check if script exists
if (-not (Test-Path $scriptPath)) {
Write-Host "ERROR: Script not found at $scriptPath" -ForegroundColor Red
exit 1
}
# Remove old task if exists
$existing = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
if ($existing) {
Write-Host "Removing existing task..." -ForegroundColor Yellow
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
}
# Create the action
$action = New-ScheduledTaskAction `
-Execute "cmd.exe" `
-Argument "/c `"$scriptPath`"" `
-WorkingDirectory $scriptDir
# Trigger: Daily at specified time
$trigger = New-ScheduledTaskTrigger -Daily -At $TriggerTime
# Settings: retry on failure, do NOT start when missed
$settings = New-ScheduledTaskSettingsSet `
-RestartCount 3 `
-RestartInterval (New-TimeSpan -Minutes 1) `
-ExecutionTimeLimit (New-TimeSpan -Minutes 5) `
-DontStopIfGoingOnBatteries `
-AllowStartIfOnBatteries
# Run as current user (no admin needed)
$principal = New-ScheduledTaskPrincipal `
-UserId "$env:USERDOMAIN\$env:USERNAME" `
-LogonType Interactive `
-RunLevel Limited
# Register
Register-ScheduledTask `
-TaskName $TaskName `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-Principal $principal `
-Description $description | Out-Null
Write-Host ""
Write-Host "Task registered successfully!" -ForegroundColor Green
Write-Host " Name: $TaskName"
Write-Host " Schedule: Daily at $TriggerTime"
Write-Host " Script: $scriptPath"
Write-Host " Log: $(Join-Path $scriptDir 'trigger.log')"
Write-Host ""
Write-Host "To test now, run:" -ForegroundColor Cyan
Write-Host " Start-ScheduledTask -TaskName '$TaskName'"
Write-Host ""
Write-Host "To remove later, run:" -ForegroundColor Cyan
Write-Host " Unregister-ScheduledTask -TaskName '$TaskName' -Confirm:`$false"