Advertisement
Sedrowow

serverstarter

Feb 14th, 2025 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ===============================================
  2. # StartServers.ps1
  3. #
  4. # Description:
  5. #   - Searches inside the "swds\servers" folder (located alongside this script)
  6. #     for subfolders starting with "server" (e.g., server123).
  7. #   - In each such folder, it looks for a "config_data" folder, and if found,
  8. #     removes the "saves" and "working_server" subfolders.
  9. #   - Then it starts a "server64.exe" (if present) from that server folder,
  10. #     each in a new window and with the argument:
  11. #         +server_dir C:\swds\servers\{serverFolderName}\config_data
  12. #   - Logs each step to a "latest.log" file inside a "logs" folder (next to the script).
  13. #   - Displays a hint ("Press 'q' to quit...") and waits for the user to press "q".
  14. #     When "q" is pressed, it terminates all the servers started by this script.
  15. #
  16. # Note: Run this script with appropriate privileges if required.
  17. # ===============================================
  18.  
  19. # --- Define log file location ---
  20.  
  21. # $PSScriptRoot is the directory in which the script resides.
  22. $scriptDir = $PSScriptRoot
  23. $logDir    = Join-Path $scriptDir "logs"
  24.  
  25. # Create logs folder if it does not exist.
  26. if (-not (Test-Path $logDir)) {
  27.     New-Item -Path $logDir -ItemType Directory | Out-Null
  28. }
  29.  
  30. $logFile = Join-Path $logDir "latest.log"
  31.  
  32. # Function to log messages with timestamps.
  33. function Write-Log {
  34.     param (
  35.         [string]$Message
  36.     )
  37.     $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
  38.     $line = "$timestamp - $Message"
  39.     Write-Output $line
  40.     Add-Content -Path $logFile -Value $line
  41. }
  42.  
  43. Write-Log "Script started."
  44.  
  45. # --- Locate the swds\servers folder ---
  46.  
  47. $serversPath = Join-Path $scriptDir "swds\servers"
  48. if (-not (Test-Path $serversPath)) {
  49.     Write-Log "ERROR: 'swds\servers' directory does not exist at path: $serversPath. Exiting."
  50.     exit
  51. }
  52.  
  53. # --- Get server folders ---
  54. # Filter for directories whose names start with "server" followed by digits.
  55. $serverFolders = Get-ChildItem -Path $serversPath -Directory | Where-Object { $_.Name -match "^server\d+" }
  56. Write-Log "Found $($serverFolders.Count) server folder(s) in $serversPath."
  57.  
  58. # --- Array to store started server processes ---
  59. $serverProcesses = @()
  60.  
  61. # --- Process each server folder ---
  62. foreach ($serverFolder in $serverFolders) {
  63.     Write-Log "Processing folder: $($serverFolder.FullName)"
  64.    
  65.     # Look for the config_data folder
  66.     $configDataPath = Join-Path $serverFolder.FullName "config_data"
  67.     if (Test-Path $configDataPath) {
  68.         Write-Log "Found 'config_data' folder at: $configDataPath"
  69.        
  70.         # Define paths for the subfolders to remove.
  71.         $savesPath         = Join-Path $configDataPath "saves"
  72.         $workingServerPath = Join-Path $configDataPath "working_server"
  73.        
  74.         # Remove "saves" folder if it exists.
  75.         if (Test-Path $savesPath) {
  76.             try {
  77.                 Remove-Item -Path $savesPath -Recurse -Force
  78.                 Write-Log "Removed folder: $savesPath"
  79.             }
  80.             catch {
  81.                 Write-Log "ERROR: Failed to remove folder: $savesPath. Error details: $_"
  82.             }
  83.         }
  84.         else {
  85.             Write-Log "Folder does not exist (skipped): $savesPath"
  86.         }
  87.        
  88.         # Remove "working_server" folder if it exists.
  89.         if (Test-Path $workingServerPath) {
  90.             try {
  91.                 Remove-Item -Path $workingServerPath -Recurse -Force
  92.                 Write-Log "Removed folder: $workingServerPath"
  93.             }
  94.             catch {
  95.                 Write-Log "ERROR: Failed to remove folder: $workingServerPath. Error details: $_"
  96.             }
  97.         }
  98.         else {
  99.             Write-Log "Folder does not exist (skipped): $workingServerPath"
  100.         }
  101.     }
  102.     else {
  103.         Write-Log "'config_data' folder not found in: $($serverFolder.FullName)"
  104.     }
  105.    
  106.     # --- Start the server executable with the +server_dir argument ---
  107.     $serverExePath = Join-Path $serverFolder.FullName "server64.exe"
  108.     if (Test-Path $serverExePath) {
  109.         try {
  110.             # Build the server_dir argument using the absolute path
  111.             $serverDirArgument = Join-Path -Path "C:\swds\servers" -ChildPath "$($serverFolder.Name)\config_data"
  112.             Write-Log "Starting server: $serverExePath with argument '+server_dir $serverDirArgument'"
  113.             # Start the server64.exe in its folder with the argument.
  114.             $process = Start-Process -FilePath $serverExePath `
  115.                                      -WorkingDirectory $serverFolder.FullName `
  116.                                      -ArgumentList "+server_dir", "$serverDirArgument" `
  117.                                      -PassThru
  118.             $serverProcesses += $process
  119.             Write-Log "Server started with Process ID: $($process.Id)"
  120.         }
  121.         catch {
  122.             Write-Log "ERROR: Failed to start server: $serverExePath. Error details: $_"
  123.         }
  124.     }
  125.     else {
  126.         Write-Log "Executable not found: $($serverFolder.FullName)\server64.exe"
  127.     }
  128. }
  129.  
  130. Write-Log "All servers started (if available)."
  131. Write-Output "Press 'q' to quit and stop all servers."
  132.  
  133. # --- Monitor for 'q' key press to exit ---
  134. while ($true) {
  135.     if ($Host.UI.RawUI.KeyAvailable) {
  136.         $key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  137.         if ($key.Character -eq 'q' -or $key.Character -eq 'Q') {
  138.             Write-Log "'q' pressed. Initiating shutdown of all started servers..."
  139.             break
  140.         }
  141.     }
  142.     Start-Sleep -Milliseconds 200
  143. }
  144.  
  145. # --- Stop all started server processes ---
  146. foreach ($proc in $serverProcesses) {
  147.     try {
  148.         Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
  149.         Write-Log "Stopped server process with ID: $($proc.Id)"
  150.     }
  151.     catch {
  152.         Write-Log "ERROR: Failed to stop process with ID: $($proc.Id). Error details: $_"
  153.     }
  154. }
  155.  
  156. Write-Log "Script finished. Exiting."
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement