Advertisement
Ubidibity

Run-DayZ-Loop.ps1

Jan 25th, 2025 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -AssemblyName System.Windows.Forms
  2.  
  3. # uses several other scripts to maintain a week of incremental backups taken every time the server restarts with a new
  4. # GUI popup status bar to track the steps between boots.  Replaces the previous 'runme.bat' file moving everything into powershell.
  5. # References to dependencies for successful use:
  6. # Periodic reboot schedule update to DayZ config: https://pastebin.com/1KVDnc4D
  7. # Powershell for daily dayz incremental backups https://pastebin.com/ibf8ephZ
  8. # Powershell to limit incremental zips to one week https://pastebin.com/muwsXgG8
  9. # Powershell to clean zips older than the desired retention period https://pastebin.com/muwsXgG8
  10. # Powershell to analyze output zip and display summary to powershell console https://pastebin.com/wDntDwJT
  11.  
  12. # 20250126 edit: PowerShell doesn't use goto, that was copied from the original .bat in error.  I've converted it to a while true
  13. # loop.  I also added a double line write-host to help visually see the different loop output on the powershell console.
  14.  
  15. # Create a custom PowerShell object for the form
  16. $formObject = [PSCustomObject]@{
  17.     Form = $null
  18.     Label = $null
  19.     ProgressBar = $null
  20.     UpdateProgress = {
  21.         param($progress)
  22.         if ($this.ProgressBar -ne $null) {
  23.             $this.ProgressBar.Value = $progress
  24.         }
  25.     }
  26.     UpdateLabel = {
  27.         param($text)
  28.         if ($this.Label -ne $null) {
  29.             $this.Label.Text = $text
  30.         }
  31.     }
  32. }
  33.  
  34. # Method to initialize the form
  35. function Initialize-Form {
  36.     $formObject.Form = New-Object System.Windows.Forms.Form
  37.     $formObject.Form.Text = 'Progress Window'
  38.     $formObject.Form.Size = New-Object System.Drawing.Size(300,200)
  39.     $formObject.Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
  40.     $formObject.Form.TopMost = $true
  41.  
  42.     $formObject.Label = New-Object System.Windows.Forms.Label
  43.     $formObject.Label.Text = 'Starting...'
  44.     $formObject.Label.AutoSize = $true
  45.     $formObject.Label.Location = New-Object System.Drawing.Point(10,20)
  46.     $formObject.Form.Controls.Add($formObject.Label)
  47.  
  48.     $formObject.ProgressBar = New-Object System.Windows.Forms.ProgressBar
  49.     $formObject.ProgressBar.Style = "Continuous"
  50.     $formObject.ProgressBar.Minimum = 0
  51.     $formObject.ProgressBar.Maximum = 100
  52.     $formObject.ProgressBar.Location = New-Object System.Drawing.Point(10,50)
  53.     $formObject.ProgressBar.Size = New-Object System.Drawing.Size(260,23)
  54.     $formObject.Form.Controls.Add($formObject.ProgressBar)
  55.  
  56.     $formObject.Form.Show()
  57. }
  58.  
  59. # Method to dispose of the form
  60. function Dispose-Form {
  61.     if ($formObject.Form -ne $null) {
  62.         $formObject.Form.Close()
  63.         $formObject.Form.Dispose()
  64.     }
  65. }
  66.  
  67. # Change directory to where DayZ server is located
  68. Set-Location -Path "C:\dayz-server"
  69.  
  70. #:goon (this was pronounced 'go on')
  71. while ($true) {
  72.  
  73. # Initialize the form
  74. Initialize-Form
  75.  
  76. $i = 0
  77. $text = "Cleaning zips..."
  78. $updateLabel = { param($text) $formObject.Label.Text = $text }
  79. $updateProgress = { param($progress) $formObject.ProgressBar.Value = $progress }
  80.    
  81. & $updateLabel -text "Processing: $i%"
  82. & $updateProgress -progress $i
  83.     [System.Windows.Forms.Application]::DoEvents()
  84.  
  85. # Prune then Back-up DayZ between executions
  86. Write-Host "Cleaning zips..."
  87. & "c:\users\administrator\documents\clean-zips.ps1"
  88.  
  89. $i = 50
  90. $text = "Incremental backup..."
  91. $updateLabel = { param($text) $formObject.Label.Text = $text }
  92. $updateProgress = { param($progress) $formObject.ProgressBar.Value = $progress }
  93.    
  94. & $updateLabel -text "Processing: $i%"
  95. & $updateProgress -progress $i
  96.     [System.Windows.Forms.Application]::DoEvents()
  97.  
  98. # Write-Host "Backing up..."
  99. & "c:\users\administrator\documents\dayz-bak.ps1"
  100.  
  101. $i = 100
  102. $text = "Check for updates..."
  103. $updateLabel = { param($text) $formObject.Label.Text = $text }
  104. $updateProgress = { param($progress) $formObject.ProgressBar.Value = $progress }
  105.    
  106. & $updateLabel -text "Processing: $i%"
  107. & $updateProgress -progress $i
  108.     [System.Windows.Forms.Application]::DoEvents()
  109.  
  110. # Check for updates
  111. # Write-Host "Checking for updates..."
  112. & "c:\dayz-server\UpdateDayz-onceaday.ps1"
  113.  
  114. # Cleanup
  115. Dispose-Form
  116.  
  117. # Run the DayZ server
  118. Write-Host "Running the server..."
  119. Write-Host "====================="
  120. Start-Process -FilePath "DayZServer_x64.exe" -ArgumentList "-config=serverDZ.cfg", "-port=2302", "--dologs", "-adminlog", "-netlog", "-freezecheck", "-profiles=c:\dayz-server\DayZServerprofiles" -Wait
  121.  
  122. # Loop back to start if the server crashes or stops
  123. # goto :goon
  124. }
Tags: #DayZ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement