Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ===============================================
- # StartServers.ps1
- #
- # Description:
- # - Searches inside the "swds\servers" folder (located alongside this script)
- # for subfolders starting with "server" (e.g., server123).
- # - In each such folder, it looks for a "config_data" folder, and if found,
- # removes the "saves" and "working_server" subfolders.
- # - Then it starts a "server64.exe" (if present) from that server folder,
- # each in a new window and with the argument:
- # +server_dir C:\swds\servers\{serverFolderName}\config_data
- # - Logs each step to a "latest.log" file inside a "logs" folder (next to the script).
- # - Displays a hint ("Press 'q' to quit...") and waits for the user to press "q".
- # When "q" is pressed, it terminates all the servers started by this script.
- #
- # Note: Run this script with appropriate privileges if required.
- # ===============================================
- # --- Define log file location ---
- # $PSScriptRoot is the directory in which the script resides.
- $scriptDir = $PSScriptRoot
- $logDir = Join-Path $scriptDir "logs"
- # Create logs folder if it does not exist.
- if (-not (Test-Path $logDir)) {
- New-Item -Path $logDir -ItemType Directory | Out-Null
- }
- $logFile = Join-Path $logDir "latest.log"
- # Function to log messages with timestamps.
- function Write-Log {
- param (
- [string]$Message
- )
- $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
- $line = "$timestamp - $Message"
- Write-Output $line
- Add-Content -Path $logFile -Value $line
- }
- Write-Log "Script started."
- # --- Locate the swds\servers folder ---
- $serversPath = Join-Path $scriptDir "swds\servers"
- if (-not (Test-Path $serversPath)) {
- Write-Log "ERROR: 'swds\servers' directory does not exist at path: $serversPath. Exiting."
- exit
- }
- # --- Get server folders ---
- # Filter for directories whose names start with "server" followed by digits.
- $serverFolders = Get-ChildItem -Path $serversPath -Directory | Where-Object { $_.Name -match "^server\d+" }
- Write-Log "Found $($serverFolders.Count) server folder(s) in $serversPath."
- # --- Array to store started server processes ---
- $serverProcesses = @()
- # --- Process each server folder ---
- foreach ($serverFolder in $serverFolders) {
- Write-Log "Processing folder: $($serverFolder.FullName)"
- # Look for the config_data folder
- $configDataPath = Join-Path $serverFolder.FullName "config_data"
- if (Test-Path $configDataPath) {
- Write-Log "Found 'config_data' folder at: $configDataPath"
- # Define paths for the subfolders to remove.
- $savesPath = Join-Path $configDataPath "saves"
- $workingServerPath = Join-Path $configDataPath "working_server"
- # Remove "saves" folder if it exists.
- if (Test-Path $savesPath) {
- try {
- Remove-Item -Path $savesPath -Recurse -Force
- Write-Log "Removed folder: $savesPath"
- }
- catch {
- Write-Log "ERROR: Failed to remove folder: $savesPath. Error details: $_"
- }
- }
- else {
- Write-Log "Folder does not exist (skipped): $savesPath"
- }
- # Remove "working_server" folder if it exists.
- if (Test-Path $workingServerPath) {
- try {
- Remove-Item -Path $workingServerPath -Recurse -Force
- Write-Log "Removed folder: $workingServerPath"
- }
- catch {
- Write-Log "ERROR: Failed to remove folder: $workingServerPath. Error details: $_"
- }
- }
- else {
- Write-Log "Folder does not exist (skipped): $workingServerPath"
- }
- }
- else {
- Write-Log "'config_data' folder not found in: $($serverFolder.FullName)"
- }
- # --- Start the server executable with the +server_dir argument ---
- $serverExePath = Join-Path $serverFolder.FullName "server64.exe"
- if (Test-Path $serverExePath) {
- try {
- # Build the server_dir argument using the absolute path
- $serverDirArgument = Join-Path -Path "C:\swds\servers" -ChildPath "$($serverFolder.Name)\config_data"
- Write-Log "Starting server: $serverExePath with argument '+server_dir $serverDirArgument'"
- # Start the server64.exe in its folder with the argument.
- $process = Start-Process -FilePath $serverExePath `
- -WorkingDirectory $serverFolder.FullName `
- -ArgumentList "+server_dir", "$serverDirArgument" `
- -PassThru
- $serverProcesses += $process
- Write-Log "Server started with Process ID: $($process.Id)"
- }
- catch {
- Write-Log "ERROR: Failed to start server: $serverExePath. Error details: $_"
- }
- }
- else {
- Write-Log "Executable not found: $($serverFolder.FullName)\server64.exe"
- }
- }
- Write-Log "All servers started (if available)."
- Write-Output "Press 'q' to quit and stop all servers."
- # --- Monitor for 'q' key press to exit ---
- while ($true) {
- if ($Host.UI.RawUI.KeyAvailable) {
- $key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
- if ($key.Character -eq 'q' -or $key.Character -eq 'Q') {
- Write-Log "'q' pressed. Initiating shutdown of all started servers..."
- break
- }
- }
- Start-Sleep -Milliseconds 200
- }
- # --- Stop all started server processes ---
- foreach ($proc in $serverProcesses) {
- try {
- Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
- Write-Log "Stopped server process with ID: $($proc.Id)"
- }
- catch {
- Write-Log "ERROR: Failed to stop process with ID: $($proc.Id). Error details: $_"
- }
- }
- Write-Log "Script finished. Exiting."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement