-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_scheduled_task.ps1
55 lines (46 loc) · 1.79 KB
/
create_scheduled_task.ps1
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
# Check if running as admin and self-elevate if needed
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
Write-Host "Requesting administrative privileges..." -ForegroundColor Yellow
$arguments = "-File `"$($MyInvocation.MyCommand.Path)`""
Start-Process powershell -Verb RunAs -ArgumentList $arguments
exit
}
# Get absolute paths
$scriptPath = $PSScriptRoot
$pythonExe = Join-Path $scriptPath ".venv\Scripts\python.exe"
$refreshPy = Join-Path $scriptPath "refresh.py"
try {
# Remove existing task if it exists
Get-ScheduledTask -TaskName "OkeBet Refresh" -ErrorAction SilentlyContinue |
Unregister-ScheduledTask -Confirm:$false
# Create task configuration
$action = New-ScheduledTaskAction `
-Execute $pythonExe `
-Argument "`"$refreshPy`"" `
-WorkingDirectory $scriptPath
$trigger = New-ScheduledTaskTrigger -Daily -At 6AM
$settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-StartWhenAvailable `
-WakeToRun `
-ExecutionTimeLimit (New-TimeSpan -Minutes 30) `
-RestartCount 3 `
-RestartInterval (New-TimeSpan -Minutes 1)
$principal = New-ScheduledTaskPrincipal `
-UserId "SYSTEM" `
-LogonType ServiceAccount `
-RunLevel Highest
Register-ScheduledTask `
-TaskName "OkeBet Refresh" `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-Principal $principal `
-Force
Write-Host "Task created successfully!" -ForegroundColor Green
}
catch {
Write-Host "Error creating task: $_" -ForegroundColor Red
}
Read-Host -Prompt "Press Enter to exit"