|
| 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