-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule-docker-server-launcher.ps1
More file actions
88 lines (70 loc) · 3.11 KB
/
schedule-docker-server-launcher.ps1
File metadata and controls
88 lines (70 loc) · 3.11 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Get the script's directory and filename without the .ps1 extension
$scriptPath = $MyInvocation.MyCommand.Path
$scriptDir = Split-Path -Parent $scriptPath
$scriptKey = [IO.Path]::GetFileNameWithoutExtension($scriptPath)
# Path to the config file
$configFilePath = Join-Path -Path $scriptDir -ChildPath "config.json"
# Check if the config file exists
if (-Not (Test-Path -Path $configFilePath)) {
Write-Error "Configuration file not found at $configFilePath."
# Prevent the window from closing after the program ends
Write-Host "Press any key to close this window..."
[void][System.Console]::ReadKey()
exit 1
}
# Load and parse the JSON config file
try {
$configData = Get-Content -Path $configFilePath -Raw | ConvertFrom-Json
} catch {
Write-Error "Failed to parse the configuration file as JSON: $_"
# Prevent the window from closing after the program ends
Write-Host "Press any key to close this window..."
[void][System.Console]::ReadKey()
exit 1
}
# Extract the section corresponding to the script's filename
if (-Not $configData.$scriptKey) {
Write-Error "The '$scriptKey' section is missing in the configuration file."
# Prevent the window from closing after the program ends
Write-Host "Press any key to close this window..."
[void][System.Console]::ReadKey()
exit 1
}
$config = $configData.$scriptKey
# Define variables
$taskName = $config.taskName
$taskDescription = $config.taskDescription
$exePath = $config.exePath
$exeArguments = $config.exeArguments # Add arguments if needed, otherwise leave empty
if (-not $isAdmin) {
Write-Host "Restarting PowerShell as administrator..."
Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
# Prevent the window from closing after the program ends
Write-Host "Press any key to close this window..."
[void][System.Console]::ReadKey()
exit
}
# Check if Task Scheduler already has the task
if (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) {
Write-Output "Task '$taskName' already exists. Deleting existing task..."
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
}
# Create a new task action
$action = New-ScheduledTaskAction -Execute $exePath -Argument $exeArguments
# Set the trigger to run at system startup
$trigger = New-ScheduledTaskTrigger -AtStartup
# Specify task settings
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
# Register the task with the local system account
Register-ScheduledTask -TaskName $taskName `
-Description $taskDescription `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-User "SYSTEM" `
-RunLevel Highest
Write-Output "Task '$taskName' has been added to Task Scheduler and will run at system boot."
# Prevent the window from closing after the program ends
Write-Host "Press any key to close this window..."
[void][System.Console]::ReadKey()
exit