Skip to content

Instantly share code, notes, and snippets.

@blbrdv
Last active September 21, 2023 10:01
Show Gist options
  • Save blbrdv/7709c178f0c82c6aedb0f965ddb6b793 to your computer and use it in GitHub Desktop.
Save blbrdv/7709c178f0c82c6aedb0f965ddb6b793 to your computer and use it in GitHub Desktop.
Run cs 1.6 server as Windows service
#Requires -RunAsAdministrator
Add-Type -assemblyname System.ServiceProcess;
$CurrentDir = $((Get-Location).Path);
$global:Time = "00:00:10";
$global:Mapcycle = "$CurrentDir\cstrike\mapcycle.txt";
function StopServ
{
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,Position=0)]
[ServiceProcess.ServiceController]$Service
)
if ( $Service.Status -eq "Stopped" ) {
Write-Host "Service" $Service.Name "already stopped";
return;
}
Write-Host "Stopping service" $Service.Name;
$Service.Stop() | Out-Null;
$Service.WaitForStatus("Stopped", $global:Time) | Out-Null;
}
function StartServ
{
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,Position=0)]
[ServiceProcess.ServiceController]$Service
)
if ( $Service.Status -eq "Running" ) {
Write-Host "Service" $Service.Name "already running";
return;
}
Write-Host "Starting service" $Service.Name;
$Service.Start() | Out-Null;
$Service.WaitForStatus("Running", $global:Time) | Out-Null;
}
function GetOrCreateServ
{
[CmdletBinding()]
[OutputType([ServiceProcess.ServiceController])]
param (
[Parameter(Mandatory=$true,Position=0)]
[hashtable]$Params
)
$Service = Get-Service -Name $Params.Name -ErrorAction SilentlyContinue;
if ( $null -eq $Service )
{
Write-Host "Creating service" $Params.Name;
nssm install $Params.Name $Params.Path | Out-Null;
nssm set $Params.Name DisplayName $Params.DisplayName | Out-Null;
if ( $null -ne $Params.DependsOn )
{
nssm set $Params.Name DependOnService $Params.DependsOn | Out-Null;
}
nssm set $Params.Name AppStdout $Params.Log | Out-Null;
nssm set $Params.Name AppStderr $Params.Log | Out-Null;
nssm set $Params.Name AppStdoutCreationDisposition 4 | Out-Null;
nssm set $Params.Name AppStderrCreationDisposition 4 | Out-Null;
nssm set $Params.Name AppRotateFiles 1 | Out-Null;
nssm set $Params.Name AppRotateOnline 0 | Out-Null;
nssm set $Params.Name AppRotateSeconds 86400 | Out-Null;
nssm set $Params.Name AppRotateBytes 1048576 | Out-Null;
nssm set $Params.Name Start "SERVICE_DEMAND_START" | Out-Null;
$Service = Get-Service -Name $Params.Name;
}
else
{
StopServ $Service;
}
nssm set $Params.Name Description "$($Params.DisplayName) ----- $($Params.Args)" | Out-Null;
nssm set $Params.Name AppParameters $Params.Args | Out-Null;
StartServ $Service;
return $Service;
}
function RandomiseMaps {
if ( -not ( Test-Path -Path "$CurrentDir\hlds.exe" -PathType Leaf ) )
{
Write-Error -Message "Script must be placed in HLDS installation directory" -ErrorAction Stop;
}
if ( -not ( Test-Path -Path $global:Mapcycle -PathType Leaf ) )
{
Write-Error -Message "File 'mapcycle.txt' does not exists" -ErrorAction Stop;
}
Get-Content $global:Mapcycle | Sort-Object { Get-Random } | Set-Content $global:Mapcycle;
}
function GetStartingMap {
Get-Content -Path $global:Mapcycle -Tail 1
}
Write-Host "Starting...";
RandomiseMaps
$Map = GetStartingMap
Write-Host " - CurrentDir = $CurrentDir";
Write-Host " - Map = $Map";
Write-Host " - Time = $global:Time";
Write-Host;
if ( -not ( Test-Path -Path "$CurrentDir\hlds.exe" -PathType Leaf ) )
{
Write-Error -Message "Script must be placed in HLDS installation directory" -ErrorAction Stop;
}
$CSServer = GetOrCreateServ @{
Name = "CSServer"
Path = "$CurrentDir\hlds.exe"
Args = "-console -game cstrike +ip 0.0.0.0 +port 27016 +sys_ticrate 10000 +maxplayers 32 +map $Map"
DisplayName = "CS 1.6 server"
Log = "$CurrentDir\logs\hlds.log"
}
$FDServer = GetOrCreateServ @{
Name = "CSFD"
Path = "sfk"
Args = "ftpserv -ownip 0.0.0.0 -rw $CurrentDir\cstrike"
DisplayName = "SFK FTP server"
DependsOn = "CSServer"
Log = "$CurrentDir\logs\sfk.log"
}
Write-Host;
Read-Host -Prompt "Press Enter to stop CS server";
StopServ $FDServer;
StopServ $CSServer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment