Skip to content

Commit

Permalink
ready for release
Browse files Browse the repository at this point in the history
  • Loading branch information
patrix87 committed Dec 11, 2022
1 parent be1df90 commit c8a48a4
Show file tree
Hide file tree
Showing 38 changed files with 113 additions and 171 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# PowerShellGSM - PowerShell Game Server Manager
A Modular PowerShell tool to manage game servers.

This PowerShell script will Install, Backup, Update and Start your server when executed.
This PowerShell script will take care of:
- Installation
- Backups
- Updates
- Monitoring
- Restarts

You can create a Scheduled Task to execute run.cmd daily to execute your server maintenance.

If you enable Auto-Updates in a Server configuration, a Scheduled Task will automatically be created to check for new updates every 15 minutes.
Once started it registers a schedule task to check on your server status.

# Supported Games

Expand Down Expand Up @@ -34,7 +37,6 @@ If you enable Auto-Updates in a Server configuration, a Scheduled Task will auto

- Faster than the other available tools
- Lighter and more targeted backups
- Lighter shared game servers installation
- Modular
- Expandable
- Customizable
Expand Down
2 changes: 1 addition & 1 deletion functions/process/Get-Lock.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function Get-Lock {
[OutputType([boolean])]
param (
)
if ((Test-Path -Path ".\servers\$($Server.UID).LOCK" -PathType "Leaf" -ErrorAction SilentlyContinue)) {
if ((Test-Path -Path ".\servers\$($Server.Name).LOCK" -PathType "Leaf" -ErrorAction SilentlyContinue)) {
return $true
}
return $false
Expand Down
2 changes: 1 addition & 1 deletion functions/process/Get-PID.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Get-PID {
)
try {
#Read the process ID from the PID file named by the UID defined in the server cfg file.
$ServerPID = Get-Content -Path ".\servers\$($Server.UID).PID" -ErrorAction SilentlyContinue
$ServerPID = Get-Content -Path ".\servers\$($Server.Name).PID" -ErrorAction SilentlyContinue
}
catch {
return $null
Expand Down
2 changes: 1 addition & 1 deletion functions/process/Lock-Process.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function Lock-Process {
param (
)
try {
$null = New-Item -Path ".\servers\" -Name "$($Server.UID).LOCK" -ItemType "file" -Force -ErrorAction SilentlyContinue
$null = New-Item -Path ".\servers\" -Name "$($Server.Name).LOCK" -ItemType "file" -Force -ErrorAction SilentlyContinue
Write-ScriptMsg "Process Locked."
}
catch {
Expand Down
2 changes: 1 addition & 1 deletion functions/process/Register-PID.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Register-PID {
}
$stopwatch.Stop()
try {
$null = New-Item -Path ".\servers\" -Name "$($Server.UID).PID" -ItemType "file" -Value "$($ServerProcess.ID)" -Force -ErrorAction SilentlyContinue
$null = New-Item -Path ".\servers\" -Name "$($Server.Name).PID" -ItemType "file" -Value "$($ServerProcess.ID)" -Force -ErrorAction SilentlyContinue
Write-ScriptMsg "Process Registered."
}
catch {
Expand Down
2 changes: 1 addition & 1 deletion functions/process/Unlock-Process.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Unlock-Process {
)
try {
#Delete the LOCK file based on the Server UID.
$null = Remove-Item -Path ".\servers\$($Server.UID).LOCK" -Confirm:$false -ErrorAction SilentlyContinue
$null = Remove-Item -Path ".\servers\$($Server.Name).LOCK" -Confirm:$false -ErrorAction SilentlyContinue
Write-ScriptMsg "Process Unlocked."
}
catch {
Expand Down
2 changes: 1 addition & 1 deletion functions/process/Unregister-PID.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Unregister-PID {
)
try {
#Delete the PID file based on the Server UID.
$null = Remove-Item -Path ".\servers\$($Server.UID).PID" -Confirm:$false -ErrorAction SilentlyContinue
$null = Remove-Item -Path ".\servers\$($Server.Name).PID" -Confirm:$false -ErrorAction SilentlyContinue
}
catch {
return $false
Expand Down
11 changes: 2 additions & 9 deletions functions/server/Request-Update.psm1
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
function Request-Update {
$ServerProcess = Get-ServerProcess
if (-not ($ServerProcess)){
Exit-WithError -ErrorMsg "Server is not active."
}
if (-not ($Server.AutoUpdates)){
Exit-WithError -ErrorMsg "AutoUpdates are disabled."
}
#Create server directory if not found.
if (-not (Test-Path -Path $Server.Path -ErrorAction SilentlyContinue)){
$null = New-Item -ItemType "directory" -Path $Server.Path -ErrorAction SilentlyContinue
Expand All @@ -29,9 +22,9 @@ function Request-Update {
#Join each item of the list with an LF
$FileContent = $ArgumentList -join "`n"
#Define the Script file name
$ScriptFile = "SteamCMD_Update_$($Server.UID).txt"
$ScriptFile = "SteamCMD_Update_$($Server.Name).txt"
#Define the Script file name
$UpdateReturnFile = "SteamCMD_Update_$($Server.UID)_Return.txt"
$UpdateReturnFile = "SteamCMD_Update_$($Server.Name)_Return.txt"
#Define the full path.
$ScriptPath = (Resolve-CompletePath -Path ".\servers\$ScriptFile" -ParentPath ".\servers\")
#Create the script.
Expand Down
2 changes: 1 addition & 1 deletion functions/server/Update-Server.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function Update-Server {
#Join each item of the list with an LF
$FileContent = $ArgumentList -join "`n"
#Define the Script file name
$ScriptFile = "SteamCMD_$($Server.UID).txt"
$ScriptFile = "SteamCMD_$($Server.Name).txt"
#Define the full path.
$ScriptPath = (Resolve-CompletePath -Path ".\servers\$ScriptFile" -ParentPath ".\servers\")
#Create the script.
Expand Down
4 changes: 2 additions & 2 deletions functions/util/Exit-WithError.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ function Exit-WithError
)
#Write error in red on black, stop logging, pause, exit.
Write-Host -ForegroundColor "Red" -BackgroundColor "Black" -Object $ErrorMsg
Unlock-Process
Stop-Transcript
$null = Unlock-Process
$null = Stop-Transcript
if ($Global.PauseOnErrors){
Read-Host "Press Enter to close this window."
}
Expand Down
14 changes: 7 additions & 7 deletions functions/util/Get-TaskConfig.psm1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function Get-TaskConfig {
Write-ScriptMsg "Getting Tasks Schedule..."
$NextAlive = Get-IniValue -file ".\servers\$($Server.UID).INI" -key "NextAlive"
$NextUpdate = Get-IniValue -file ".\servers\$($Server.UID).INI" -key "NextUpdate"
$NextRestart = Get-IniValue -file ".\servers\$($Server.UID).INI" -key "NextRestart"
return {
NextAlive: $NextAlive,
NextUpdate: $NextUpdate,
NextRestart: $NextRestart
$NextAlive = Get-IniValue -file ".\servers\$($Server.Name).INI" -category "Schedule" -key "NextAlive"
$NextUpdate = Get-IniValue -file ".\servers\$($Server.Name).INI" -category "Schedule" -key "NextUpdate"
$NextRestart = Get-IniValue -file ".\servers\$($Server.Name).INI" -category "Schedule" -key "NextRestart"
return [hashtable] @{
NextAlive=$NextAlive;
NextUpdate=$NextUpdate;
NextRestart=$NextRestart;
}
}
Export-ModuleMember -Function Get-TaskConfig
4 changes: 2 additions & 2 deletions functions/util/Register-Task.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ function Register-Task {
$action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-WindowStyle Hidden -NoProfile -ExecutionPolicy Bypass -File $scriptpath -ServerCfg `"$ServerCfg`" -Task" -WorkingDirectory $dir
$trigger = New-ScheduledTaskTrigger -Daily -At 12am -RandomDelay (New-TimeSpan -Minutes $Global.TaskCheckFrequency)
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 10)
$description = "Run Tasks for $($server.UID)"
$title = "Tasks-$($server.UID)"
$description = "Run Tasks for $($server.Name)"
$title = "Tasks-$($server.Name)"
$task = New-ScheduledTask -Description $description -Action $action -Trigger $trigger -Settings $settings
$RegisteredTask = Register-ScheduledTask $title -InputObject $task
$RegisteredTask.Triggers.Repetition.Duration = "P1D" #Repeat for a duration of one day
Expand Down
2 changes: 1 addition & 1 deletion functions/util/Register-TaskConfig.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function Register-TaskConfig {
Write-ScriptMsg "Registering Tasks Schedule..."
try {
$null = New-Item -Path ".\servers\" -Name "$($Server.UID).INI" -ItemType "file" -Force -ErrorAction SilentlyContinue
$null = New-Item -Path ".\servers\" -Name "$($Server.Name).INI" -ItemType "file" -Force -ErrorAction SilentlyContinue
Write-ScriptMsg "Tasks Schedule Registered."
}
catch {
Expand Down
2 changes: 1 addition & 1 deletion functions/util/Remove-TaskConfig.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Remove-TaskConfig {
)
try {
#Delete the INI file based on the Server UID.
$null = Remove-Item -Path ".\servers\$($Server.UID).INI" -Confirm:$false -ErrorAction SilentlyContinue
$null = Remove-Item -Path ".\servers\$($Server.Name).INI" -Confirm:$false -ErrorAction SilentlyContinue
Write-ScriptMsg "Task Config Removed."
}
catch {
Expand Down
10 changes: 5 additions & 5 deletions functions/util/Unregister-Task.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Unregister-UpdateTask {
$title = "Tasks-$($server.UID)"
$null = Unregister-ScheduledTask -TaskName $title -Confirm:$false -ErrorAction SilentlyContinue
Remove-TaskConfig
function Unregister-Task {
$title = "Tasks-$($server.Name)"
$null = Unregister-ScheduledTask -TaskName $title -Confirm:$false
$null = Remove-TaskConfig
}
Export-ModuleMember -Function Unregister-UpdateTask
Export-ModuleMember -Function Unregister-Task
8 changes: 4 additions & 4 deletions functions/util/Update-TaskConfig.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ function Update-TaskConfig {
[Parameter(Mandatory=$false)]
[switch]$Update,
[Parameter(Mandatory=$false)]
[string]$Restart
[switch]$Restart
)

Write-ScriptMsg "Updating Tasks Schedule..."

if($Alive){
$NextAlive = (Get-Date).AddMinutes($Global.AliveCheckFrequency)
Set-IniValue -file ".\servers\$($Server.UID).INI" -key "NextAlive" -value $NextAlive
Set-IniValue -file ".\servers\$($Server.Name).INI" -category "Schedule" -key "NextAlive" -value $NextAlive
}

if($Update){
$NextUpdate = (Get-Date).AddMinutes($Global.UpdateCheckFrequency)
Set-IniValue -file ".\servers\$($Server.UID).INI" -key "NextUpdate" -value $NextUpdate
Set-IniValue -file ".\servers\$($Server.Name).INI" -category "Schedule" -key "NextUpdate" -value $NextUpdate
}

if($Restart){
$NextRestart = (Get-Date -Hour $Server.AutoRestartTime[0] -Minute $Server.AutoRestartTime[1] -Second $Server.AutoRestartTime[2]).AddDays(1)
Set-IniValue -file ".\servers\$($Server.UID).INI" -key "NextRestart" -value $NextRestart
Set-IniValue -file ".\servers\$($Server.Name).INI" -category "Schedule" -key "NextRestart" -value $NextRestart
}
}
Export-ModuleMember -Function Update-TaskConfig
2 changes: 1 addition & 1 deletion global.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $GlobalDetails = @{
UpdateCheckFrequency = 15

#Check if the server is alive Frequency in Minutes
AliveCheckFrequency = 5
AliveCheckFrequency = 2

#Should be lower or equal to the two above
TaskCheckFrequency = 2
Expand Down
50 changes: 30 additions & 20 deletions main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ try {
}
catch {
Exit-WithError -ErrorMsg "Unable to import modules."
exit
Exit
}

#---------------------------------------------------------
Expand Down Expand Up @@ -65,7 +65,9 @@ Write-ScriptMsg "Importing Server Configuration..."
#Check if requested config exist in the config folder, if not, copy it from the templates. Exit if fails.
#In the case of an update check or alive check, remove the check if the configuration is deleted.
if (-not (Test-Path -Path ".\configs\$ServerCfg.psm1" -PathType "Leaf" -ErrorAction SilentlyContinue)) {
$Server = New-Object -TypeName PsObject -Property @{Name=$ServerCfg}
if($Task){
Write-ScriptMsg "Server Configuration no longer exists, unregistering Tasks..."
Unregister-Task
Exit
}
Expand All @@ -89,50 +91,58 @@ Read-Config

#Check if script is already running
if(Get-Lock){
exit
Write-ScriptMsg "Process is locked, exiting."
Exit
}

#Locking Script to avoid double run
Lock-Process
$null = Lock-Process

#---------------------------------------------------------
# Checking Scheduled Task
#---------------------------------------------------------
if ($Task) {
$FullRunRequired = $false
Write-ScriptMsg "Running Tasks for $($Server.UID) ..."
$TasksSchedule = Get-TaskConfig

if(($Server.AutoRestartOnCrash) -and (($TasksSchedule.NextAlive) -lt (Get-Date))){
Write-ScriptMsg "Running Tasks for $($ServerCfg) ..."
$TasksSchedule = (Get-TaskConfig)
if(($Server.AutoRestartOnCrash) -and (($TasksSchedule.NextAlive) -le (Get-Date))){
Write-ScriptMsg "Checking Alive State"
if(-not (Get-ServerProcess)) {
Write-ScriptMsg "Server is Dead, Restarting..."
Update-TaskConfig -Alive
$FullRunRequired = $true
} else {
Write-ScriptMsg "Server is Alive"
}
Update-TaskConfig -Alive
} else {
Write-ScriptMsg "Too soon for Alive check"
}

if(($Server.AutoUpdates) -and (($TasksSchedule.NextUpdate) -lt (Get-Date))){
Write-ScriptMsg "Checking on steamCMD if updates are avaiable for $($Server.UID)..."
if(($Server.AutoUpdates) -and (($TasksSchedule.NextUpdate) -le (Get-Date))){
Write-ScriptMsg "Checking on steamCMD if updates are avaiable for $($Server.Name)..."
if (Request-Update){
Write-ScriptMsg "Updates are available for $($Server.UID), Proceeding with update process..."
Write-ScriptMsg "Updates are available for $($Server.Name), Proceeding with update process..."
$FullRunRequired = $true
Update-TaskConfig -Update
} else {
Write-ScriptMsg "No updates are available for $($Server.UID)"
Write-ScriptMsg "No updates are available for $($Server.Name)"
}
Update-TaskConfig -Update
} else {
Write-ScriptMsg "Too soon for Update check"
}

Write-ScriptMsg "Checking if server $($Server.UID) is due for restart"
if(($Server.AutoRestart) -and (($TasksSchedule.NextRestart) -lt (Get-Date))){
if(($Server.AutoRestart) -and (($TasksSchedule.NextRestart) -le (Get-Date))){
Write-ScriptMsg "Server is due for Restart"
$FullRunRequired = $true
Update-TaskConfig -Restart
} else {
Write-ScriptMsg "Too soon for Restart"
}

if(-not $FullRunRequired) {
exit
$null = Unlock-Process
Write-ScriptMsg "No tasks ready, exiting."
Exit
}
#Run Launcher as usual
}
Expand Down Expand Up @@ -186,8 +196,8 @@ if (-not $FreshInstall -and $Server.AutoUpdates) {
# Register Scheduled Task
#---------------------------------------------------------

if ($Server.AutoUpdates -and -not (Get-ScheduledTask -TaskName "Tasks-$($server.UID)" -ErrorAction SilentlyContinue)) {
Write-ScriptMsg "Registering Scheduled Tasks Check for $($Server.UID)..."
if (($Server.AutoUpdates -or $Server.AutoRestartOnCrash -or $Server.AutoRestart) -and -not (Get-ScheduledTask -TaskName "Tasks-$($server.Name)" -ErrorAction SilentlyContinue)) {
Write-ScriptMsg "Registering Scheduled Tasks Check for $($Server.Name)..."
Register-Task
}

Expand Down Expand Up @@ -229,12 +239,12 @@ catch {
# Unlock Process
#---------------------------------------------------------

Unlock-Process
$null = Unlock-Process

Write-ServerMsg "Script successfully completed."

#---------------------------------------------------------
# Stop Logging
#---------------------------------------------------------

Stop-Transcript
$null = Stop-Transcript
7 changes: 2 additions & 5 deletions templates/7daystodie.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
#Change your servers settings in C:\Users\%username%\AppData\Roaming\7DaysToDie\Saves\serverconfig.xml
#>

#Server Name, use the same name to share game files.
$Name = "7DaysToDie"
#Server Name, Always Match the Launcher and config file name.
$Name = $ServerCfg

#---------------------------------------------------------
# Server Configuration
#---------------------------------------------------------

$ServerDetails = @{

#Unique Identifier used to track processes. Must be unique to each servers.
UID = "7DaysToDie_1"

#Login username used by SteamCMD
Login = "anonymous"

Expand Down
7 changes: 2 additions & 5 deletions templates/astroneer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
https://blog.astroneer.space/p/astroneer-dedicated-server-details/
#>

#Server Name, use the same name to share game files.
$Name = "Astroneer"
#Server Name, Always Match the Launcher and config file name.
$Name = $ServerCfg

#---------------------------------------------------------
# Server Configuration
#---------------------------------------------------------

$ServerDetails = @{

#Unique Identifier used to track processes. Must be unique to each servers.
UID = "Astroneer_1"

#Login username used by SteamCMD
Login = "anonymous"

Expand Down
Loading

0 comments on commit c8a48a4

Please sign in to comment.