From 2a5f34e1cded9636cbadf9d13c1158d8febd047c Mon Sep 17 00:00:00 2001 From: Patrick Veilleux Date: Wed, 3 Nov 2021 20:57:38 -0400 Subject: [PATCH] Improve Update Task --- functions/server/Get-ServerProcess.psm1 | 19 +++++++++++++++++++ functions/server/Request-Update.psm1 | 7 +++++++ functions/server/Stop-Server.psm1 | 14 ++------------ main.ps1 | 2 +- 4 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 functions/server/Get-ServerProcess.psm1 diff --git a/functions/server/Get-ServerProcess.psm1 b/functions/server/Get-ServerProcess.psm1 new file mode 100644 index 0000000..4fcf146 --- /dev/null +++ b/functions/server/Get-ServerProcess.psm1 @@ -0,0 +1,19 @@ +function Get-ServerProcess { + if ($Server.UsePID){ + #Get the PID from the .PID market file. + $ServerPID = Get-PID + #If it returned 0, it failed to get a PID + if ($null -ne $ServerPID) { + $ServerProcess = Get-Process -ID $ServerPID -ErrorAction SilentlyContinue + } + } else { + # Find the process by name. + $ServerProcess = Get-Process -Name $Server.ProcessName -ErrorAction SilentlyContinue + } + #Check if the process was found. + if (-not ($null -eq $ServerProcess)) { + return $ServerProcess + } + return $false +} +Export-ModuleMember -Function Get-ServerProcess \ No newline at end of file diff --git a/functions/server/Request-Update.psm1 b/functions/server/Request-Update.psm1 index d7b7ad1..c71ccb2 100644 --- a/functions/server/Request-Update.psm1 +++ b/functions/server/Request-Update.psm1 @@ -1,4 +1,11 @@ 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 diff --git a/functions/server/Stop-Server.psm1 b/functions/server/Stop-Server.psm1 index b338d4b..d1ea741 100644 --- a/functions/server/Stop-Server.psm1 +++ b/functions/server/Stop-Server.psm1 @@ -1,17 +1,7 @@ function Stop-Server { - if ($Server.UsePID){ - #Get the PID from the .PID market file. - $ServerPID = Get-PID - #If it returned 0, it failed to get a PID - if ($null -ne $ServerPID) { - $ServerProcess = Get-Process -ID $ServerPID -ErrorAction SilentlyContinue - } - } else { - # Find the process by name. - $ServerProcess = Get-Process -Name $Server.ProcessName -ErrorAction SilentlyContinue - } + $ServerProcess = Get-ServerProcess #Check if the process was found. - if ($null -eq $ServerProcess) { + if (-not ($ServerProcess)) { Write-ServerMsg "Server is not running." } else { #Check if it's the right server via RCON if possible. diff --git a/main.ps1 b/main.ps1 index cb75bb6..783f968 100644 --- a/main.ps1 +++ b/main.ps1 @@ -86,7 +86,7 @@ Read-Config #--------------------------------------------------------- # Checking if updates are available. #--------------------------------------------------------- -if ($UpdateCheck -and $server.AutoUpdates) { +if ($UpdateCheck) { Write-ScriptMsg "Checking on steamCMD if updates are avaiable for $($Server.Name)..." if (-not (Request-Update)){ Write-ScriptMsg "No updates are available for $($Server.Name)"