<# --------------------------------------------------------------------------- Module......: Configuration.psm1 Projekt.....: Veeam Enterprise Installer Version.....: 2.0.0 Beschreibung Verwaltung aller JSON-Konfigurationsdateien. Funktionen: • JSON laden • Konfiguration prüfen • Standardwerte setzen • Konfiguration bereitstellen • SQL / Domain / Logging Einstellungen laden ---------------------------------------------------------------------------#> Set-StrictMode -Version Latest #region Variablen $Script:ConfigurationData = @{} $Script:ConfigurationLoaded = $false #endregion ############################################################ # Read-JsonFile ############################################################ function Read-JsonFile { <# .SYNOPSIS Liest eine JSON-Datei. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { throw "Konfigurationsdatei nicht gefunden: $Path" } try { $Content = Get-Content ` -Path $Path ` -Raw ` -Encoding UTF8 return ( $Content | ConvertFrom-Json ) } catch { throw ` "Fehler beim Lesen der JSON Datei: $Path" } } ############################################################ # Test-JsonFile ############################################################ function Test-JsonFile { <# .SYNOPSIS Prüft JSON Datei. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { return $false } try { Get-Content ` $Path ` -Raw | ConvertFrom-Json | Out-Null return $true } catch { return $false } } ############################################################ # Get-ConfigurationPath ############################################################ function Get-ConfigurationPath { param( [Parameter(Mandatory)] [string]$FileName ) $Paths = Get-ProjectPaths Join-Path ` $Paths.Config ` $FileName } #Beginn PART2 ############################################################ # Import-VeeamConfiguration ############################################################ function Import-VeeamConfiguration { <# .SYNOPSIS Lädt die Hauptkonfiguration. #> param( [string]$Path ) if([string]::IsNullOrWhiteSpace($Path)) { $Path = Get-ConfigurationPath ` -FileName "VeeamConfig.json" } Write-Verbose ` "Lade Veeam Konfiguration" if(!(Test-JsonFile -Path $Path)) { throw ` "VeeamConfig.json ist nicht vorhanden oder ungültig." } $Configuration = Read-JsonFile ` -Path $Path Set-Configuration ` -Configuration $Configuration Set-ProjectConfiguration ` -Configuration $Configuration return $Configuration } ############################################################ # Import-SQLConfiguration ############################################################ function Import-SQLConfiguration { <# .SYNOPSIS Lädt SQL Einstellungen. #> $File = Get-ConfigurationPath ` -FileName "SQLConfig.json" if(!(Test-JsonFile $File)) { throw ` "SQLConfig.json ist nicht vorhanden oder ungültig." } Read-JsonFile ` -Path $File } ############################################################ # Import-DomainConfiguration ############################################################ function Import-DomainConfiguration { <# .SYNOPSIS Lädt Domänenkonfiguration. #> $File = Get-ConfigurationPath ` -FileName "DomainConfig.json" if(!(Test-JsonFile $File)) { Write-Warning ` "Keine DomainConfig.json gefunden." return $null } Read-JsonFile ` -Path $File } ############################################################ # Import-LoggingConfiguration ############################################################ function Import-LoggingConfiguration { <# .SYNOPSIS Lädt Logging Einstellungen. #> $File = Get-ConfigurationPath ` -FileName "LoggingConfig.json" if(!(Test-JsonFile $File)) { return $null } Read-JsonFile ` -Path $File } ############################################################ # Import-PluginConfiguration ############################################################ function Import-PluginConfiguration { <# .SYNOPSIS Lädt Plugin Einstellungen. #> $File = Get-ConfigurationPath ` -FileName "Plugins.json" if(!(Test-JsonFile $File)) { return $null } Read-JsonFile ` -Path $File } #Beginn PART 3 ############################################################ # Import-AllConfiguration ############################################################ function Import-AllConfiguration { <# .SYNOPSIS Lädt alle Konfigurationsdateien. #> Write-Host "Lade Konfiguration..." $Configuration = Import-VeeamConfiguration Write-Host ` "Konfiguration erfolgreich geladen." return $Configuration } ############################################################ # Get-Configuration ############################################################ function Get-Configuration { <# .SYNOPSIS Liefert die komplette Konfiguration. #> if(!$Script:ConfigurationLoaded) { throw ` "Konfiguration wurde noch nicht geladen." } return $Script:ConfigurationData } ############################################################ # Test-ConfigurationLoaded ############################################################ function Test-ConfigurationLoaded { return $Script:ConfigurationLoaded } ############################################################ # Set-Configuration ############################################################ function Set-Configuration { param( [Parameter(Mandatory)] [psobject]$Configuration ) $Script:ConfigurationData = $Configuration $Script:ConfigurationLoaded = $true } ############################################################ # Clear-Configuration ############################################################ function Clear-Configuration { $Script:ConfigurationData = @{} $Script:ConfigurationLoaded = $false } #Beginn PART 4 ############################################################ # Get-ConfigurationValue ############################################################ function Get-ConfigurationValue { <# .SYNOPSIS Liest einen Wert aus der Konfiguration. .DESCRIPTION Unterstützt Punktnotation. Beispiel: SQL.Instance #> param( [Parameter(Mandatory)] [string]$Path ) if(!$Script:ConfigurationLoaded) { throw ` "Konfiguration wurde nicht geladen." } $Value = $Script:ConfigurationData foreach($Part in $Path.Split(".")) { if($null -eq $Value) { return $null } $Property = $Value.PSObject.Properties[$Part] if($null -eq $Property) { return $null } $Value = $Property.Value } return $Value } ############################################################ # Set-ConfigurationValue ############################################################ function Set-ConfigurationValue { <# .SYNOPSIS Ändert einen Konfigurationswert. #> param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] $Value ) if(!$Script:ConfigurationLoaded) { throw "Konfiguration wurde nicht geladen." } $Parts = $Path.Split(".") $Object = $Script:ConfigurationData for($i=0; $i -lt $Parts.Count-1; $i++) { $Property = $Object.PSObject.Properties[$Parts[$i]] if($null -eq $Property -or $null -eq $Property.Value) { throw "Konfigurationspfad nicht gefunden: $Path" } $Object = $Property.Value } $TargetProperty = $Object.PSObject.Properties[$Parts[-1]] if($null -eq $TargetProperty) { $Object | Add-Member ` -MemberType NoteProperty ` -Name $Parts[-1] ` -Value $Value } else { $TargetProperty.Value = $Value } } ############################################################ # Test-RequiredConfiguration ############################################################ function Test-RequiredConfiguration { <# .SYNOPSIS Prüft Pflichtwerte. #> param( [string[]]$RequiredValues ) $Missing = @() foreach($ValuePath in $RequiredValues) { $Value = Get-ConfigurationValue ` -Path $ValuePath if( $null -eq $Value -or [string]::IsNullOrWhiteSpace( [string]$Value ) ) { $Missing += $ValuePath } } if($Missing.Count -gt 0) { return [PSCustomObject]@{ Valid = $false Missing = $Missing } } return [PSCustomObject]@{ Valid = $true Missing = @() } } ############################################################ # Add-ConfigurationDefault ############################################################ function Add-ConfigurationDefault { <# .SYNOPSIS Fügt Standardwerte hinzu. #> param( [string]$Path, $Value ) $Existing = Get-ConfigurationValue ` -Path $Path if($null -eq $Existing) { Set-ConfigurationValue ` -Path $Path ` -Value $Value } } #Beginn PART 5 ############################################################ # Test-VeeamConfiguration ############################################################ function Test-VeeamConfiguration { <# .SYNOPSIS Prüft Veeam spezifische Konfiguration. #> param( [psobject]$Configuration ) if($null -eq $Configuration) { $Configuration = Get-Configuration } $Required = @( "Veeam.Version" "Veeam.SourcePath" "SQL.Server" "SQL.Instance" "SQL.Database" "Repository.Name" "Repository.Path" "BackupJob.Name" "BackupJob.Platform" "BackupJob.Schedule" ) $Missing = @() foreach($ValuePath in $Required) { $Value = $Configuration foreach($Part in $ValuePath.Split(".")) { if($null -eq $Value) { break } $Property = $Value.PSObject.Properties[$Part] if($null -eq $Property) { $Value = $null break } $Value = $Property.Value } if($null -eq $Value -or [string]::IsNullOrWhiteSpace([string]$Value)) { $Missing += $ValuePath } } $BackupJobProperty = $Configuration.PSObject.Properties["BackupJob"] if( $null -eq $BackupJobProperty -or $null -eq $BackupJobProperty.Value.PSObject.Properties["Objects"] ) { $Missing += "BackupJob.Objects" } if( $null -eq $BackupJobProperty -or $null -eq $BackupJobProperty.Value.PSObject.Properties["ScheduleGroups"] -or @($BackupJobProperty.Value.ScheduleGroups).Count -eq 0 ) { $Missing += "BackupJob.ScheduleGroups" } return [PSCustomObject]@{ Valid = ($Missing.Count -eq 0) Missing = $Missing } } ############################################################ # Test-SQLConfiguration ############################################################ function Test-SQLConfiguration { <# .SYNOPSIS Prüft SQL Einstellungen. #> $Required = @( "SQL.Server", "SQL.Instance", "SQL.Database" ) Test-RequiredConfiguration ` -RequiredValues $Required } ############################################################ # Test-DomainConfiguration ############################################################ function Test-DomainConfiguration { <# .SYNOPSIS Prüft Domänenkonfiguration. #> $Domain = Get-ConfigurationValue ` -Path "Domain.Enabled" if($Domain -eq $false) { return [PSCustomObject]@{ Valid = $true Message = "Keine Domänenintegration aktiviert." } } $Required = @( "Domain.Name", "Domain.ServiceAccount" ) Test-RequiredConfiguration ` -RequiredValues $Required } ############################################################ # Test-PluginConfiguration ############################################################ function Test-PluginConfiguration { <# .SYNOPSIS Prüft Plugin Einstellungen. #> $Plugins = Get-ConfigurationValue ` -Path "Plugins" if($null -eq $Plugins) { return [PSCustomObject]@{ Valid = $true Message = "Keine Plugin-Konfiguration vorhanden." } } return [PSCustomObject]@{ Valid = $true Plugins = $Plugins } } ############################################################ # Test-PathConfiguration ############################################################ function Test-PathConfiguration { <# .SYNOPSIS Prüft Installationspfade. #> $Paths = @( "Veeam.InstallPath", "Veeam.CatalogPath", "Veeam.TempPath" ) $Result = @() foreach($Path in $Paths) { $Value = Get-ConfigurationValue ` -Path $Path if($null -ne $Value) { $Result += [PSCustomObject]@{ Path = $Value Exists = Test-Path $Value } } } return $Result } ############################################################ # Validate-Configuration ############################################################ function Validate-Configuration { <# .SYNOPSIS Gesamte Konfiguration prüfen. #> $Results = [ordered]@{} $Results.Veeam = Test-VeeamConfiguration $Results.SQL = Test-SQLConfiguration $Results.Domain = Test-DomainConfiguration $Results.Plugins = Test-PluginConfiguration $Results.Paths = Test-PathConfiguration return [PSCustomObject]$Results } #Beginn PART 6 ############################################################ # Get-ConfigurationSummary ############################################################ function Get-ConfigurationSummary { <# .SYNOPSIS Erstellt eine Übersicht der geladenen Konfiguration. #> if(!$Script:ConfigurationLoaded) { throw ` "Keine Konfiguration geladen." } [PSCustomObject]@{ Loaded = $Script:ConfigurationLoaded Sections = $Script:ConfigurationData.PSObject.Properties.Name LoadTime = Get-Date } } ############################################################ # Export-Configuration ############################################################ function Export-Configuration { <# .SYNOPSIS Exportiert die aktuelle Konfiguration. #> param( [string]$Path ) if([string]::IsNullOrWhiteSpace($Path)) { $Path = Join-Path ` (Get-ProjectPaths).Reports ` "ConfigurationExport.json" } $Script:ConfigurationData | ConvertTo-Json ` -Depth 10 | Set-Content ` -Path $Path ` -Encoding UTF8 return $Path } ############################################################ # Import-ConfigurationFromFile ############################################################ function Import-ConfigurationFromFile { <# .SYNOPSIS Lädt eine komplette Konfiguration aus einer Datei. #> param( [Parameter(Mandatory)] [string]$Path ) $Data = Read-JsonFile ` -Path $Path Set-Configuration ` -Configuration $Data return $Data } ############################################################ # Test-ConfigurationIntegrity ############################################################ function Test-ConfigurationIntegrity { <# .SYNOPSIS Prüft Grundstruktur. #> if(!$Script:ConfigurationLoaded) { return $false } if($null -eq $Script:ConfigurationData) { return $false } return $true } ############################################################ # Get-VeeamConfig ############################################################ function Get-VeeamConfig { <# .SYNOPSIS Lädt zentrale Veeam Konfiguration aus JSON. #> param( [string]$Path ) Import-VeeamConfiguration ` -Path $Path } ############################################################ # Test-VeeamConfig ############################################################ function Test-VeeamConfig { <# .SYNOPSIS Prüft Konfigurationswerte. #> param( [Parameter(Mandatory)] [PSCustomObject]$Configuration ) Test-VeeamConfiguration ` -Configuration $Configuration } ############################################################ # Export Module Functions ############################################################ Export-ModuleMember ` -Function ` Read-JsonFile, Test-JsonFile, Get-ConfigurationPath, Import-VeeamConfiguration, Import-SQLConfiguration, Import-DomainConfiguration, Import-LoggingConfiguration, Import-PluginConfiguration, Import-AllConfiguration, Get-Configuration, Test-ConfigurationLoaded, Set-Configuration, Clear-Configuration, Get-ConfigurationValue, Set-ConfigurationValue, Test-RequiredConfiguration, Add-ConfigurationDefault, Test-VeeamConfiguration, Test-SQLConfiguration, Test-DomainConfiguration, Test-PluginConfiguration, Test-PathConfiguration, Validate-Configuration, Get-ConfigurationSummary, Export-Configuration, Import-ConfigurationFromFile, Test-ConfigurationIntegrity ############################################################ # Ende Configuration.psm1 ############################################################