Skip to content

Commit bc387a9

Browse files
committed
feat: Add PowerShell script to create and run CircleCI Runner Agent as a scheduled task
1 parent 599c51e commit bc387a9

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

.circleci/create_runner_task.ps1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Define the path to your script
2+
$scriptPath = $PSScriptRoot + "\Start-CircleCIRunner.ps1"
3+
4+
# Task name and description
5+
$taskName = "CircleCI Runner Agent"
6+
$taskDescription = "Automatically runs the CircleCI Runner Agent at startup."
7+
8+
# Get the current user
9+
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
10+
11+
# Check if the task already exists
12+
$existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
13+
14+
# If the task exists, remove it
15+
if ($existingTask) {
16+
Write-Host "Task '$taskName' already exists. Removing the existing task..."
17+
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
18+
19+
# Wait a moment to ensure the task is fully removed
20+
Start-Sleep -Seconds 2
21+
}
22+
23+
# Create a new action to run the script with PowerShell
24+
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File `"$scriptPath`""
25+
26+
# Create a trigger to start the task at system startup
27+
$trigger = New-ScheduledTaskTrigger -AtStartup
28+
29+
# Create a new principal to run the task under the current user account with the highest privileges
30+
$principal = New-ScheduledTaskPrincipal -UserId $currentUser -LogonType Interactive -RunLevel Highest
31+
32+
# Create the scheduled task settings (run only if idle, allow start on batteries)
33+
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries
34+
35+
# Create the scheduled task
36+
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings -TaskName $taskName -Description $taskDescription
37+
38+
Write-Host "Scheduled task '$taskName' has been created successfully!"
39+
40+
# Run the task immediately
41+
Write-Host "Starting the task immediately..."
42+
Start-ScheduledTask -TaskName $taskName
43+
44+
Write-Host "Task '$taskName' started successfully!"

0 commit comments

Comments
 (0)