Skip to content

Commit 7318bcc

Browse files
authored
Create schedule-start-server.ps1
1 parent 2f6c0f1 commit 7318bcc

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

schedule-start-server.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Define variables
2+
$taskName = "VanderstackDockerServer"
3+
$taskDescription = "Run Vanderstack Docker Server on system boot"
4+
$exePath = "E:\vanderstack-docker-server\vanderstack-docker-server.exe"
5+
$exeArguments = "" # Add arguments if needed, otherwise leave empty
6+
7+
if (-not $isAdmin) {
8+
Write-Host "Restarting PowerShell as administrator..."
9+
Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
10+
11+
# Prevent the window from closing after the program ends
12+
Write-Host "Press any key to close this window..."
13+
[void][System.Console]::ReadKey()
14+
exit
15+
}
16+
17+
# Check if Task Scheduler already has the task
18+
if (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) {
19+
Write-Output "Task '$taskName' already exists. Deleting existing task..."
20+
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
21+
}
22+
23+
# Create a new task action
24+
$action = New-ScheduledTaskAction -Execute $exePath -Argument $exeArguments
25+
26+
# Set the trigger to run at system startup
27+
$trigger = New-ScheduledTaskTrigger -AtStartup
28+
29+
# Specify task settings
30+
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
31+
32+
# Register the task with the local system account
33+
Register-ScheduledTask -TaskName $taskName `
34+
-Description $taskDescription `
35+
-Action $action `
36+
-Trigger $trigger `
37+
-Settings $settings `
38+
-User "SYSTEM" `
39+
-RunLevel Highest
40+
41+
Write-Output "Task '$taskName' has been added to Task Scheduler and will run at system boot."
42+
43+
# Prevent the window from closing after the program ends
44+
Write-Host "Press any key to close this window..."
45+
[void][System.Console]::ReadKey()
46+
exit

0 commit comments

Comments
 (0)