Added creating INI-file.

This commit is contained in:
2024-11-29 20:10:51 +03:00
parent 206599127c
commit 1f731756fd
3 changed files with 195 additions and 128 deletions

View File

@@ -7,16 +7,79 @@
[CmdletBinding()]
param (
[Parameter()]
[int]
$param = 0
[int]$param = 0
)
[bool]$update = $param
[bool]$noDelete = $false # For tests - don't delete logs
[string]$logFile = ".\wDayzManager.log"
[string]$steamScript = "wDayzManager.txt"
[string]$iniFile = ".\wDayzManager.ini"
if (-not (Test-Path $iniFile)) {
Write-Error "Can't find file '$iniFile'"
###############################################################################################################################################################
[string]$myName = "wDayzManager"
[string]$steamScript = $myName + ".txt"
[string]$iniFile = $myName + ".ini"
if (-not (Test-Path -Path $iniFile)) {
Write-Warning "Can't find file '$iniFile', creating new..."
New-Item -ItemType File -Path $iniFile -Value "; $($iniFile)" | Out-Null
$content = "
[Config]
serverLocation=Z:\Servers\DayzServer
profilesLocation=profiles
steamCMD=Z:\SteamCMD
steamUser=SupaMegaGamer
appidGame=221100
appidServer=223350
rotateLogs=1
daysAmount=7
rotatedLogsFolderName=RotatedLogs
becFolderName=BEC
atFolderName=VPPAdminTools
; Server updating settings:
; D = Daily - Updates are checked daily at the hours specified in parameter 'updateHour' (0 - 23). In this case, parameter 'updateDay' is ignored.
; W = Weekly - Updates are checked weekly at the hours specified in parameter 'updateHour' (0 - 23). Parameter 'updateDay' specifies the day of the week (0 - 6, 0 = Sunday).
; M = Monthly - Updates are checked monthly at the hours specified in parameter 'updateHour' (0 - 23). Parameter 'updateDay' specifies the day of the month (1 - 31. Be careful with February!).
; Any other value disables the updates.
updatePeriod=W
updateDay=1
updateHour=4
[ClientMods]
@CF=1559212036
@Namalsk_Island=2288339650
@Namalsk_Survival=2288336145
[ServerMods]
[Server-1]
serverPort=2302
serverConfig=serverDZ.cfg
serverExeName=DayZServer_x64.exe
serverCPU=2
startupClientMods=@CF
startupServerMods=
additionalParams= -name=Server
; Mission name for Namalsk Mod
; This is the name of the Namalsk mod mission. It can be 'Hardcore' or 'Regular', i.e. the names of folders in the '@Namalsk_Survival\Extras\' directory.
; If you are not using Namalsk mod, this parameter is ignored.
namalskMission=
[Server-2]
serverPort=2402
serverConfig=serverDZ.cfg
serverExeName=DayZServer_x64.exe
serverCPU=2
startupClientMods=@CF;@Namalsk_Island;@Namalsk_Survival
startupServerMods=
additionalParams= -name=Server -AdminLog -FilePatching
; Mission name for Namalsk Mod
; This is the name of the Namalsk mod mission. It can be 'Hardcore' or 'Regular', i.e. the names of folders in the '@Namalsk_Survival\Extras\' directory.
; If you are not using Namalsk mod, this parameter is ignored.
namalskMission=Hardcore
"
Add-Content -Path $iniFile -Value $content -Encoding UTF8
$content = $null
Exit
}
@@ -31,17 +94,19 @@ Write-Host "update = $($update), noDelete = $($noDelete)"
$ini = $config = $servers = $clientMods = $serverMods = $steamProcess = $null
$modNI = $modNS = $namalskMission = $pathNamalskMission = $null
$dayzProcess = $becProcess = $instance = $null
$dayzProcess = $becProcess = $instance = $profilesLocation = $null
$becLocation = $rotatedLogsFolderName = $atFolderName = $serverLocation = $null
$steamCMD = $steamUser = $appidServer = $appidGame = $rotateLogs = $daysAmount = $null
$serverPort = $serverConfig = $serverExeName = $serverCPU = $null
$startupClientMods = $startupServerMods = $startupMods = $startupParams = $null
$serverProfile = $additionalParams = $dayzArguments = $dayz = $bec = $becArguments = $null
$logsName = $atLogsLocation = $becLogsLocation = $rotatedLogsLocation = $null
$currentPath = Get-Location
$ini = (Get-IniContent -FilePath $iniFile)
$config = $ini.Config
[string]$serverLocation = $config.serverLocation
[string]$profilesLocation = $config.profilesLocation
[string]$steamCMD = $config.steamCMD
[string]$steamUser = $config.steamUser
[int]$appidGame = $config.appidGame
@@ -81,12 +146,10 @@ if ($update) {
Write-Host "Start updating server..."
if (-not (Test-Path $steamCMD)) {
Write-Error "Can't find folder '$steamCMD'"
Write-Error "Can't find folder '$steamCMD'!"
Exit
}
$currentPath = Get-Location
# Making Steam script
$arr = @()
$arr += "@NoPromptForPassword 1"
@@ -99,8 +162,8 @@ if ($update) {
$arr += "quit"
# Saving Steam script
"// wDayzManager.txt - Steam script" | Out-File -FilePath "$($currentPath)\$($steamScript)" -Encoding ascii
$arr | Out-File -FilePath "$($currentPath)\$($steamScript)" -Encoding ascii -Append
"// $($steamScript) - Steam script" | Out-File -FilePath "$($currentPath)\$($steamScript)" -Encoding Ascii
$arr | Out-File -FilePath "$($currentPath)\$($steamScript)" -Encoding Ascii -Append
# Run Steam script
Write-Host "Run Steam script from `"$($currentPath)\$($steamScript)`""
@@ -167,6 +230,10 @@ if ($rotateLogs) {
if (-not ([string]::IsNullOrEmpty($config.rotatedLogsFolderName))) {
$rotatedLogsFolderName = $config.rotatedLogsFolderName
}
else {
Write-Error "The 'rotatedLogsFolderName' parameter not set!"
Exit
}
}
if (-not ([string]::IsNullOrEmpty($config.becFolderName))) {
@@ -181,8 +248,12 @@ $servers += $ini.Keys.Where({ $_ -Match "^[Ss]erver[\s_-]?[0-9]+$" })
$processID = 0
$dayzProcess = [Object[]]::new($servers.Count)
$becProcess = [Object[]]::new($servers.Count)
$dayzArguments = @()
if ($becLocation) {
$becProcess = [Object[]]::new($servers.Count)
}
EXIT
foreach ($srv in $servers) {
$instance = ${srv}
@@ -222,19 +293,18 @@ foreach ($srv in $servers) {
# Rotate logs
if ($rotateLogs) {
if ($atFolderName) {
$atLogsLocation = "$($serverProfile)\$($atFolderName)"
$logsName = Get-ChildItem -Path $atLogsLocation -Directory -Filter "Log*"
$atLogsLocation += '\' + $logsName
if ($rotatedLogsFolderName) {
$rotatedLogsLocation = "$($serverProfile)\$($rotatedLogsFolderName)"
}
if ($becLocation) {
$becLogsLocation = "$($becLocation)\Log\$($instance)"
}
if ($rotatedLogsFolderName) {
$rotatedLogsLocation = "$($serverProfile)\$($rotatedLogsFolderName)"
if ($atFolderName) {
$atLogsLocation = "$($serverProfile)\$($atFolderName)"
$logsName = Get-ChildItem -Path $atLogsLocation -Directory -Filter "Log*"
$atLogsLocation += '\' + $logsName
}
If (!(Test-Path -PathType Container $rotatedLogsLocation)) {
@@ -242,13 +312,17 @@ foreach ($srv in $servers) {
Write-Host "Created new folder: $($rotatedLogsLocation)"
}
$fileList = $null
$destDelDir = "$($rotatedLogsLocation)\DeletedLogs"
$becDestDir = "$($rotatedLogsLocation)\BEC"
$atDestDir = "$($rotatedLogsLocation)\$($atFolderName)"
if ($noDelete) {
$destDelDir = "$($rotatedLogsLocation)\DeletedLogs"
If (!(Test-Path -PathType Container $destDelDir)) {
New-Item -ItemType Directory -Path $destDelDir | Out-Null
Write-Host "Created new folder: $($destDelDir)"
}
}
#region ### DayZ Game ###
Write-Host "Rotating DAYZ logs..."
$fileList = $null
$fileList = Get-Item -Path $serverProfile\*.rpt, $serverProfile\*.log, $serverProfile\*.adm, $serverProfile\*.mdmp | Where-Object { $_.LastWriteTime.Date -lt $date.Date }
# Write-Host $fileList -Separator "`n"
@@ -262,42 +336,36 @@ foreach ($srv in $servers) {
$fileList = Get-Item -Path $rotatedLogsLocation\*.rpt, $rotatedLogsLocation\*.log, $rotatedLogsLocation\*.adm, $rotatedLogsLocation\*.mdmp | Where-Object { $_.LastWriteTime -lt $date.AddDays( - ($daysAmount)) }
# Write-Host $fileList -Separator "`n"
Write-Host "Removing logs:"
if ($noDelete) {
If (!(Test-Path -PathType Container $destDelDir)) {
New-Item -ItemType Directory -Path $destDelDir | Out-Null
Write-Host "Created new folder: $($destDelDir)"
}
foreach ($file in $fileList) {
Write-Host "Removing DayZ logs:"
foreach ($file in $fileList) {
if ($noDelete) {
Move-Item -Path $file -Destination $destDelDir -Force
Write-Host "Moved $($file) to $($destDelDir)"
}
}
else {
foreach ($file in $fileList) {
else {
Remove-Item -Path $file
Write-Host "Removed $($file)!"
}
}
Write-Host "Total: $($fileList.Count)"
Write-Host ""
Write-Host
#endregion
#region ### BEC ###
if ($becDestDir) {
Write-Host "Rotating BEC logs..."
$fileList = $null
if ($becLocation) {
$becDestDir = "$($rotatedLogsLocation)\$($becFolderName)"
If (!(Test-Path -PathType Container $becDestDir)) {
New-Item -ItemType Directory -Path $becDestDir | Out-Null
Write-Host "Created new folder: $($becDestDir)"
}
Write-Host "Rotating BEC logs..."
$fileList = $null
$fileList = Get-ChildItem -Path $becLogsLocation\*.log -Recurse | Where-Object { $_.LastWriteTime.Date -lt $date.Date }
# Write-Host $fileList -Separator "`n"
Write-Host "Moving BEC's LOGs to $($becDestDir):"
Write-Host "Moving BEC logs to $($becDestDir):"
foreach ($file in $fileList) {
Move-Item -Path $file -Destination $becDestDir -Force
Write-Host "Moved $($file) to $($becDestDir)"
@@ -307,43 +375,37 @@ foreach ($srv in $servers) {
$fileList = Get-Item -Path $becDestDir\*.log | Where-Object { $_.LastWriteTime -lt $date.AddDays( - ($daysAmount)) }
# Write-Host $fileList -Separator "`n"
Write-Host "Removing BEC's LOGs:"
if ($noDelete) {
If (!(Test-Path -PathType Container $destDelDir)) {
New-Item -ItemType Directory -Path $destDelDir | Out-Null
Write-Host "Created new folder: $($destDelDir)"
}
foreach ($file in $fileList) {
Move-Item -Path $file -Destination $destDelDir
Write-Host "Removing BEC logs:"
foreach ($file in $fileList) {
if ($noDelete) {
Move-Item -Path $file -Destination $destDelDir -Force
Write-Host "Moved $($file) to $($destDelDir)"
}
}
else {
foreach ($file in $fileList) {
else {
Remove-Item -Path $file
Write-Host "Removed $($file)!"
}
}
Write-Host "Total: $($fileList.Count)"
Write-Host ""
Write-Host
}
#endregion
#region ### Admin Tool ###
if ($atDestDir) {
Write-Host "Rotating AdminTool logs..."
$fileList = $null
if ($atLogsLocation) {
$atDestDir = "$($rotatedLogsLocation)\$($atFolderName)"
If (!(Test-Path -PathType Container $atDestDir)) {
New-Item -ItemType Directory -Path $atDestDir | Out-Null
Write-Host "Created new folder: $($atDestDir)"
}
Write-Host "Rotating AdminTool logs..."
$fileList = $null
$fileList = Get-ChildItem -Path $atLogsLocation\*.txt, $atLogsLocation\*.log -Recurse | Where-Object { $_.LastWriteTime.Date -lt $date.Date }
# Write-Host $fileList -Separator "`n"
Write-Host "Moving AdminTool's LOGs to $($atDestDir):"
Write-Host "Moving AdminTool logs to $($atDestDir):"
foreach ($file in $fileList) {
Move-Item -Path $file -Destination $atDestDir -Force
Write-Host "Moved $($file) to $($atDestDir)"
@@ -353,26 +415,20 @@ foreach ($srv in $servers) {
$fileList = Get-Item -Path $atDestDir\*.txt, $atDestDir\*.log | Where-Object { $_.LastWriteTime -lt $date.AddDays( - ($daysAmount)) }
# Write-Host $fileList -Separator "`n"
Write-Host "Removing AdminTool's LOGs:"
if ($noDelete) {
If (!(Test-Path -PathType Container $destDelDir)) {
New-Item -ItemType Directory -Path $destDelDir | Out-Null
Write-Host "Created new folder: $($destDelDir)"
}
Write-Host "Removing AdminTool logs:"
foreach ($file in $fileList) {
foreach ($file in $fileList) {
if ($noDelete) {
Move-Item -Path $file -Destination $destDelDir -Force
Write-Host "Moved $($file) to $($destDelDir)"
}
}
else {
foreach ($file in $fileList) {
else {
Remove-Item -Path $file
Write-Host "Removed $($file)!"
}
}
Write-Host "Total: $($fileList.Count)"
Write-Host ""
Write-Host
}
#endregion
}