Deployment Snippet

Registry-Werte mit Logging kontrolliert setzen.

Registry-Aenderungen sind im Deployment oft unvermeidbar. Wichtig ist, Schluessel bei Bedarf anzulegen, Typen sauber zu setzen und jede Aenderung eindeutig zu loggen.

PowerShell
function Set-RegistryValueWithLog {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)] [string]$KeyPath,
        [Parameter(Mandatory)] [string]$Name,
        [Parameter(Mandatory)] [object]$Value,
        [Parameter(Mandatory)] [string]$Type,
        [Parameter(Mandatory)] [string]$LogFile
    )

    try {
        if (-not (Test-Path -LiteralPath $KeyPath)) {
            New-Item -Path $KeyPath -Force | Out-Null
        }

        New-ItemProperty -Path $KeyPath -Name $Name -Value $Value -PropertyType $Type -Force | Out-Null
        Write-Log -Path $LogFile -Component 'Registry' -Type Info -Message "Registry gesetzt: $KeyPath [$Name=$Value]"
    }
    catch {
        Write-Log -Path $LogFile -Component 'Registry' -Type Error -Message "Registry konnte nicht gesetzt werden: $KeyPath [$Name]. $($_.Exception.Message)"
        throw
    }
}