Untitled

 avatar
unknown
plain_text
9 days ago
4.9 kB
10
Indexable
[CmdletBinding()]
param()

$ErrorActionPreference = 'Stop'

$transcriptStarted = $false

try {
    $serverExe = 'C:\host\windrose-farm\R5\Binaries\Win64\WindroseServer-Win64-Shipping.exe'
    $startBat  = 'C:\host\windrose-farm\StartServerForeground.bat'

    $sourceRoot = 'C:\host\farm\windrose-farm'
    $destRoot   = 'C:\host\windrose-farm'

    $logDir = 'C:\host\farm\logs'

    $waitAfterStopSeconds   = 90
    $waitAfterDeleteSeconds = 20
    $waitAfterCopySeconds   = 20
    $waitAfterStartSeconds  = 45

    if (-not (Test-Path -LiteralPath $logDir)) {
        New-Item -ItemType Directory -Path $logDir -Force | Out-Null
    }

    $timestamp = Get-Date -Format 'yyyy-MM-dd_HH-mm-ss'
    $logFile   = Join-Path $logDir "windrose-update-$timestamp.log"
    $roboLog   = Join-Path $logDir "windrose-robocopy-$timestamp.log"

    Start-Transcript -Path $logFile -Append
    $transcriptStarted = $true

    Write-Host "==== Script Started $(Get-Date) ===="
    Write-Host "PowerShell log: $logFile"
    Write-Host "Robocopy log: $roboLog"

    if (-not (Test-Path -LiteralPath $sourceRoot)) {
        throw "Source folder not found: $sourceRoot"
    }

    $serverProcesses = Get-CimInstance Win32_Process | Where-Object {
        $_.ExecutablePath -eq $serverExe
    }

    if ($serverProcesses) {
        Write-Host "Stopping running server processes..."

        foreach ($proc in $serverProcesses) {
            Write-Host "Stopping PID $($proc.ProcessId)"
            Stop-Process -Id $proc.ProcessId -Force -ErrorAction SilentlyContinue

            try {
                Wait-Process -Id $proc.ProcessId -Timeout 30 -ErrorAction Stop
            }
            catch {
                Write-Host "PID $($proc.ProcessId) already exited or did not exit within timeout."
            }
        }
    }
    else {
        Write-Host "No running server process found."
    }

    Write-Host "Waiting $waitAfterStopSeconds seconds after server stop..."
    Start-Sleep -Seconds $waitAfterStopSeconds

    $stillRunning = Get-CimInstance Win32_Process | Where-Object {
        $_.ExecutablePath -eq $serverExe
    }

    if ($stillRunning) {
        throw "Server process is still running after stop attempt."
    }

    if (Test-Path -LiteralPath $destRoot) {
        Write-Host "Deleting all contents from destination..."

        Get-ChildItem -LiteralPath $destRoot -Force | ForEach-Object {
            Write-Host "Deleting: $($_.FullName)"
            Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction Stop
        }

        Write-Host "Waiting $waitAfterDeleteSeconds seconds after deletion..."
        Start-Sleep -Seconds $waitAfterDeleteSeconds

        $remainingFiles = Get-ChildItem -LiteralPath $destRoot -Force -ErrorAction SilentlyContinue

        if ($remainingFiles) {
            Write-Host "Remaining items:"
            $remainingFiles | ForEach-Object {
                Write-Host $_.FullName
            }

            throw "Destination folder is not empty after deletion."
        }

        Write-Host "Destination successfully cleared."
    }
    else {
        Write-Host "Destination folder does not exist. Creating it..."
        New-Item -ItemType Directory -Path $destRoot -Force | Out-Null
    }

    Write-Host "Copying fresh files from source to destination..."

    robocopy $sourceRoot $destRoot /E /R:2 /W:2 /FFT /NP `
        /XD "$sourceRoot\R5\.sentry-native" `
            "$sourceRoot\R5\Saved\Logs" `
        /LOG:$roboLog

    $roboExitCode = $LASTEXITCODE

    Write-Host "Robocopy exit code: $roboExitCode"

    if ($roboExitCode -ge 8) {
        throw "Robocopy failed with exit code $roboExitCode. See log: $roboLog"
    }

    Write-Host "File copy completed successfully."

    Write-Host "Waiting $waitAfterCopySeconds seconds after copy..."
    Start-Sleep -Seconds $waitAfterCopySeconds

    if (-not (Test-Path -LiteralPath $startBat)) {
        throw "Startup batch file not found after copy: $startBat"
    }

    Write-Host "Starting server..."

    Start-Process -FilePath "cmd.exe" `
        -ArgumentList "/c `"$startBat`"" `
        -WorkingDirectory $destRoot

    Write-Host "Waiting $waitAfterStartSeconds seconds after server start..."
    Start-Sleep -Seconds $waitAfterStartSeconds

    $started = Get-CimInstance Win32_Process | Where-Object {
        $_.ExecutablePath -eq $serverExe
    }

    if (-not $started) {
        throw "Server did not start after running startup batch."
    }

    foreach ($proc in $started) {
        Write-Host "Server started. PID $($proc.ProcessId)"
    }

    Write-Host "==== Script Completed Successfully $(Get-Date) ===="
}
catch {
    Write-Error $_
    throw
}
finally {
    if ($transcriptStarted) {
        Stop-Transcript
    }
}
Editor is loading...
Leave a Comment