Deployment Snippet

Prozesse sicher starten und Exitcodes sauber auswerten.

Viele Deployment-Probleme entstehen nicht beim Starten eines Prozesses, sondern beim falschen Umgang mit seinem Rueckgabecode. Dieses Snippet trennt Start, Warten, Logging und Exitcode-Bewertung sauber.

PowerShell
function Start-ProcessSafe {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)] [string]$FilePath,
        [string]$Arguments,
        [Parameter(Mandatory)] [string]$LogFile,
        [int[]]$SuccessCodes = @(0, 3010)
    )

    Write-Log -Path $LogFile -Component 'Process' -Type Info -Message "Starte Prozess: $FilePath $Arguments"
    $proc = Start-Process -FilePath $FilePath -ArgumentList $Arguments -Wait -PassThru

    if ($SuccessCodes -contains $proc.ExitCode) {
        Write-Log -Path $LogFile -Component 'Process' -Type Info -Message "Prozess erfolgreich beendet. ExitCode=$($proc.ExitCode)"
        return $proc.ExitCode
    }

    Write-Log -Path $LogFile -Component 'Process' -Type Error -Message "Prozess fehlgeschlagen. ExitCode=$($proc.ExitCode)"
    throw "Unerwarteter ExitCode: $($proc.ExitCode)"
}