From 284fe027960e6720551fc2743417c53ed6036bb4 Mon Sep 17 00:00:00 2001 From: Madzone Date: Tue, 11 Aug 2026 09:10:37 +0200 Subject: [PATCH] Version 1.0 of Configure --- VEEAM-CONFIGURE/Config/VeeamConfigure.json | 12 ++ .../Configure-VeeamEnvironment.ps1 | 118 ++++++++++++++++++ VEEAM-CONFIGURE/Modules/Platforms/HyperV.psm1 | 25 ++++ .../Modules/Platforms/Proxmox.psm1 | 75 +++++++++++ VEEAM-CONFIGURE/Modules/Platforms/VMware.psm1 | 25 ++++ VEEAM-CONFIGURE/Modules/Scheduling.psm1 | 53 ++++++++ VEEAM-CONFIGURE/README.txt | 26 ++++ VEEAM-CONFIGURE/Start-VeeamConfigure.ps1 | 42 +++++++ 8 files changed, 376 insertions(+) create mode 100644 VEEAM-CONFIGURE/Config/VeeamConfigure.json create mode 100644 VEEAM-CONFIGURE/Configure-VeeamEnvironment.ps1 create mode 100644 VEEAM-CONFIGURE/Modules/Platforms/HyperV.psm1 create mode 100644 VEEAM-CONFIGURE/Modules/Platforms/Proxmox.psm1 create mode 100644 VEEAM-CONFIGURE/Modules/Platforms/VMware.psm1 create mode 100644 VEEAM-CONFIGURE/Modules/Scheduling.psm1 create mode 100644 VEEAM-CONFIGURE/README.txt create mode 100644 VEEAM-CONFIGURE/Start-VeeamConfigure.ps1 diff --git a/VEEAM-CONFIGURE/Config/VeeamConfigure.json b/VEEAM-CONFIGURE/Config/VeeamConfigure.json new file mode 100644 index 0000000..dc520b6 --- /dev/null +++ b/VEEAM-CONFIGURE/Config/VeeamConfigure.json @@ -0,0 +1,12 @@ +{ + "BackupJob": { + "Name": "ServerBackup", + "ScheduleGroups": [ + { "Name": "A-F", "From": "A", "To": "F", "Time": "20:00" }, + { "Name": "G-L", "From": "G", "To": "L", "Time": "21:00" }, + { "Name": "M-R", "From": "M", "To": "R", "Time": "22:00" }, + { "Name": "S-Z", "From": "S", "To": "Z", "Time": "23:00" }, + { "Name": "Other", "From": "Other", "To": "Other", "Time": "00:00" } + ] + } +} diff --git a/VEEAM-CONFIGURE/Configure-VeeamEnvironment.ps1 b/VEEAM-CONFIGURE/Configure-VeeamEnvironment.ps1 new file mode 100644 index 0000000..7b12c44 --- /dev/null +++ b/VEEAM-CONFIGURE/Configure-VeeamEnvironment.ps1 @@ -0,0 +1,118 @@ +param( + [ValidateSet("VMware", "HyperV", "Proxmox")] + [string]$Platform, + + [string]$Server, + + [PSCredential]$Credential, + [switch]$TrustSelfSignedCertificate, + [switch]$DryRun, + [switch]$Apply, + [switch]$NonInteractive +) + +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" +$Root = $PSScriptRoot + +if($DryRun.IsPresent -and $Apply.IsPresent) { + throw "-DryRun und -Apply dürfen nicht gleichzeitig verwendet werden." +} + +if($Apply.IsPresent) { + throw "Produktivmodus ist noch gesperrt: Die echte Veeam-Jobanlage ist noch nicht implementiert." +} + +if([string]::IsNullOrWhiteSpace($Platform)) { + if($NonInteractive) { + throw "Im nicht interaktiven Modus ist -Platform erforderlich." + } + + Write-Host "" + Write-Host "Veeam Umgebungsassistent" -ForegroundColor Cyan + Write-Host "1 - VMware vCenter" + Write-Host "2 - Microsoft Hyper-V" + Write-Host "3 - Proxmox VE" + + do { + $Selection = Read-Host "Plattform auswählen (1-3)" + } until($Selection -in @("1", "2", "3")) + + $Platform = switch($Selection) { + "1" { "VMware" } + "2" { "HyperV" } + "3" { "Proxmox" } + } +} + +if([string]::IsNullOrWhiteSpace($Server)) { + if($NonInteractive) { + throw "Im nicht interaktiven Modus ist -Server erforderlich." + } + + do { + $Server = Read-Host "DNS-Name oder IP-Adresse des $Platform-Servers" + } until(![string]::IsNullOrWhiteSpace($Server)) +} + +if(!$DryRun.IsPresent -and !$Apply.IsPresent) { + $DryRun = $true +} + +Write-Host "" +Write-Host "Plattform : $Platform" -ForegroundColor Yellow +Write-Host "Server : $Server" -ForegroundColor Yellow +Write-Host "Modus : Vorschau / Dry-Run" -ForegroundColor Yellow + +Import-Module (Join-Path $Root "Modules\Scheduling.psm1") -Force -ErrorAction Stop +Import-Module (Join-Path $Root "Modules\Platforms\$Platform.psm1") -Force -ErrorAction Stop + +$ConfigPath = Join-Path $Root "Config\VeeamConfigure.json" +$Configuration = Get-Content -LiteralPath $ConfigPath -Raw | ConvertFrom-Json + +if($Platform -eq "Proxmox" -and $null -eq $Credential) { + $Credential = Get-Credential -Message "Proxmox API-Token-ID und Token-Secret eingeben" +} + +if($Platform -eq "Proxmox" -and !$TrustSelfSignedCertificate.IsPresent -and !$NonInteractive) { + $CertificateAnswer = Read-Host "Selbstsigniertes Proxmox-Zertifikat temporär akzeptieren? [j/N]" + if($CertificateAnswer -match '^(j|ja|y|yes)$') { + $TrustSelfSignedCertificate = $true + } +} + +switch($Platform) { + "VMware" { $VirtualMachines = @(Get-VMwareInventory -Server $Server) } + "HyperV" { $VirtualMachines = @(Get-HyperVInventory -Server $Server) } + "Proxmox" { + $VirtualMachines = @( + Get-ProxmoxInventory ` + -Server $Server ` + -ApiCredential $Credential ` + -TrustSelfSignedCertificate:$TrustSelfSignedCertificate + ) + } +} + +$Schedules = New-AlphabeticalSchedulePlan ` + -JobName $Configuration.BackupJob.Name ` + -VirtualMachines $VirtualMachines ` + -ScheduleGroups @($Configuration.BackupJob.ScheduleGroups) + +Write-Host "" +Write-Host "Gefundene VMs: $($VirtualMachines.Count)" -ForegroundColor Cyan + +foreach($Schedule in $Schedules) { + Write-Host ("{0,-24} {1} {2}" -f $Schedule.JobName, $Schedule.Time, ($Schedule.VirtualMachines -join ", ")) +} + +[PSCustomObject]@{ + Platform = $Platform + Server = $Server + DryRun = $DryRun.IsPresent + VirtualMachineCount = $VirtualMachines.Count + VirtualMachines = $VirtualMachines + Schedules = $Schedules + JobsCreated = $false + Message = "Inventar gelesen und Zeitplanentwurf erstellt. Keine Veeam-Jobs angelegt." +} diff --git a/VEEAM-CONFIGURE/Modules/Platforms/HyperV.psm1 b/VEEAM-CONFIGURE/Modules/Platforms/HyperV.psm1 new file mode 100644 index 0000000..26cd3c9 --- /dev/null +++ b/VEEAM-CONFIGURE/Modules/Platforms/HyperV.psm1 @@ -0,0 +1,25 @@ +Set-StrictMode -Version Latest + +function Get-HyperVInventory { + param([Parameter(Mandatory)][string]$Server) + + foreach($Name in @("Get-VBRServer", "Find-VBRHvEntity")) { + if($null -eq (Get-Command $Name -ErrorAction SilentlyContinue)) { + throw "Veeam-Befehl nicht verfügbar: $Name" + } + } + + $VBRServer = Get-VBRServer -Name $Server | + Where-Object { $_.Type -in @("HvServer", "HvCluster", "Scvmm") } | + Select-Object -First 1 + + if($null -eq $VBRServer) { + throw "Hyper-V-Server ist in Veeam nicht registriert: $Server" + } + + @(Find-VBRHvEntity -Server $VBRServer -HostsAndVMs | + Where-Object { $_.Type -match "Vm" } | + Select-Object -ExpandProperty Name -Unique) +} + +Export-ModuleMember -Function Get-HyperVInventory diff --git a/VEEAM-CONFIGURE/Modules/Platforms/Proxmox.psm1 b/VEEAM-CONFIGURE/Modules/Platforms/Proxmox.psm1 new file mode 100644 index 0000000..95709a0 --- /dev/null +++ b/VEEAM-CONFIGURE/Modules/Platforms/Proxmox.psm1 @@ -0,0 +1,75 @@ +Set-StrictMode -Version Latest + +function Get-ProxmoxInventory { + param( + [Parameter(Mandatory)][string]$Server, + [Parameter(Mandatory)][PSCredential]$ApiCredential, + [switch]$TrustSelfSignedCertificate + ) + + $Secret = $ApiCredential.GetNetworkCredential().Password + $Headers = @{ Authorization = "PVEAPIToken=$($ApiCredential.UserName)=$Secret" } + $Uri = "https://$Server`:8006/api2/json/cluster/resources?type=vm" + $PreviousProtocol = [Net.ServicePointManager]::SecurityProtocol + $PreviousCertificatePolicy = $null + $CertificatePolicyChanged = $false + $RequestParameters = @{ + Method = "Get" + Uri = $Uri + Headers = $Headers + ErrorAction = "Stop" + } + + try { + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + + if($TrustSelfSignedCertificate) { + $InvokeRestMethod = Get-Command Invoke-RestMethod -ErrorAction Stop + + if($InvokeRestMethod.Parameters.ContainsKey("SkipCertificateCheck")) { + $RequestParameters["SkipCertificateCheck"] = $true + } + else { + if($null -eq ("TemporaryTrustAllCertificatePolicy" -as [type])) { + Add-Type -TypeDefinition @" +using System.Net; +using System.Security.Cryptography.X509Certificates; + +public sealed class TemporaryTrustAllCertificatePolicy : ICertificatePolicy +{ + public bool CheckValidationResult( + ServicePoint servicePoint, + X509Certificate certificate, + WebRequest request, + int certificateProblem) + { + return true; + } +} +"@ + } + + $PreviousCertificatePolicy = [Net.ServicePointManager]::CertificatePolicy + [Net.ServicePointManager]::CertificatePolicy = + New-Object TemporaryTrustAllCertificatePolicy + $CertificatePolicyChanged = $true + } + } + + $Response = Invoke-RestMethod @RequestParameters + @($Response.data | + Where-Object { $_.type -eq "qemu" } | + Select-Object -ExpandProperty name -Unique) + } + finally { + [Net.ServicePointManager]::SecurityProtocol = $PreviousProtocol + if($CertificatePolicyChanged) { + [Net.ServicePointManager]::CertificatePolicy = $PreviousCertificatePolicy + } + $Secret = $null + $Headers = $null + $RequestParameters = $null + } +} + +Export-ModuleMember -Function Get-ProxmoxInventory diff --git a/VEEAM-CONFIGURE/Modules/Platforms/VMware.psm1 b/VEEAM-CONFIGURE/Modules/Platforms/VMware.psm1 new file mode 100644 index 0000000..07eed7d --- /dev/null +++ b/VEEAM-CONFIGURE/Modules/Platforms/VMware.psm1 @@ -0,0 +1,25 @@ +Set-StrictMode -Version Latest + +function Get-VMwareInventory { + param([Parameter(Mandatory)][string]$Server) + + foreach($Name in @("Get-VBRServer", "Find-VBRViEntity")) { + if($null -eq (Get-Command $Name -ErrorAction SilentlyContinue)) { + throw "Veeam-Befehl nicht verfügbar: $Name" + } + } + + $VBRServer = Get-VBRServer -Name $Server | + Where-Object { $_.Type -in @("VC", "ESXi", "ESX") } | + Select-Object -First 1 + + if($null -eq $VBRServer) { + throw "VMware-Server ist in Veeam nicht registriert: $Server" + } + + @(Find-VBRViEntity -Server $VBRServer -VMsAndTemplates | + Where-Object { $_.Type -match "Vm" } | + Select-Object -ExpandProperty Name -Unique) +} + +Export-ModuleMember -Function Get-VMwareInventory diff --git a/VEEAM-CONFIGURE/Modules/Scheduling.psm1 b/VEEAM-CONFIGURE/Modules/Scheduling.psm1 new file mode 100644 index 0000000..ac8bd06 --- /dev/null +++ b/VEEAM-CONFIGURE/Modules/Scheduling.psm1 @@ -0,0 +1,53 @@ +Set-StrictMode -Version Latest + +function New-AlphabeticalSchedulePlan { + param( + [Parameter(Mandatory)][string]$JobName, + [AllowEmptyCollection()][string[]]$VirtualMachines = @(), + [Parameter(Mandatory)][object[]]$ScheduleGroups + ) + + $Results = @() + foreach($Group in $ScheduleGroups) { + foreach($Property in @("Name", "From", "To", "Time")) { + if($Group.PSObject.Properties.Name -notcontains $Property) { + throw "Zeitplangruppe enthält nicht den Pflichtwert '$Property'." + } + } + + if([string]$Group.Time -notmatch '^(?:[01]\d|2[0-3]):[0-5]\d$') { + throw "Ungültige Uhrzeit in Gruppe '$($Group.Name)': $($Group.Time)" + } + + $Other = ([string]$Group.From -eq "Other") -and ([string]$Group.To -eq "Other") + $Members = @() + + foreach($VM in $VirtualMachines) { + if([string]::IsNullOrWhiteSpace($VM)) { continue } + $Initial = $VM.Substring(0, 1).ToUpperInvariant() + $Letter = $Initial -match '^[A-Z]$' + $Match = $Other -and !$Letter + + if(!$Other -and $Letter) { + $From = ([string]$Group.From).ToUpperInvariant() + $To = ([string]$Group.To).ToUpperInvariant() + $Match = ($Initial -ge $From) -and ($Initial -le $To) + } + + if($Match) { $Members += $VM } + } + + if($Members.Count -gt 0) { + $Results += [PSCustomObject]@{ + Group = [string]$Group.Name + JobName = "{0}-{1}" -f $JobName, $Group.Name + Time = [string]$Group.Time + VirtualMachines = $Members + } + } + } + + return $Results +} + +Export-ModuleMember -Function New-AlphabeticalSchedulePlan diff --git a/VEEAM-CONFIGURE/README.txt b/VEEAM-CONFIGURE/README.txt new file mode 100644 index 0000000..d9f0829 --- /dev/null +++ b/VEEAM-CONFIGURE/README.txt @@ -0,0 +1,26 @@ +VEEAM-CONFIGURE + +Dieses Projekt liest VM-Inventar und erstellt einen alphabetischen Zeitplanentwurf. +Die aktuelle Version legt keine echten Veeam-Jobs an. + +Interaktiver Start: + .\Start-VeeamConfigure.ps1 + +Dry-Run ist die Standardauswahl. Produktiv erfordert die Eingabe PRODUKTIV. +Der Produktivmodus bleibt gesperrt, bis die echte Veeam-Jobanlage implementiert ist. + +Direkter Dry-Run: + .\Configure-VeeamEnvironment.ps1 -DryRun + +VMware: + .\Configure-VeeamEnvironment.ps1 -Platform VMware -Server vcenter.domain.local -DryRun + +Hyper-V: + .\Configure-VeeamEnvironment.ps1 -Platform HyperV -Server hyperv01.domain.local -DryRun + +Proxmox: + .\Configure-VeeamEnvironment.ps1 -Platform Proxmox -Server pve01.domain.local -DryRun + +Bei Proxmox werden API-Token-ID und Token-Secret interaktiv abgefragt. +Bei einem selbstsignierten Zertifikat kann die temporäre Annahme interaktiv bestätigt werden. +Nicht interaktiv: -TrustSelfSignedCertificate diff --git a/VEEAM-CONFIGURE/Start-VeeamConfigure.ps1 b/VEEAM-CONFIGURE/Start-VeeamConfigure.ps1 new file mode 100644 index 0000000..5925a48 --- /dev/null +++ b/VEEAM-CONFIGURE/Start-VeeamConfigure.ps1 @@ -0,0 +1,42 @@ +param( + [ValidateSet("DryRun", "Production")] + [string]$Mode = "DryRun" +) + +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" +$ConfigureScript = Join-Path $PSScriptRoot "Configure-VeeamEnvironment.ps1" + +if(!$PSBoundParameters.ContainsKey("Mode")) { + Write-Host "" + Write-Host "Veeam Configure Startmenü" -ForegroundColor Cyan + Write-Host "1 - Dry-Run (Standard und empfohlen)" + Write-Host "2 - Produktiv" + + $Selection = Read-Host "Modus auswählen [1]" + if([string]::IsNullOrWhiteSpace($Selection) -or $Selection -eq "1") { + $Mode = "DryRun" + } + elseif($Selection -eq "2") { + $Mode = "Production" + } + else { + throw "Ungültige Auswahl: $Selection" + } +} + +if($Mode -eq "Production") { + Write-Host "" + Write-Warning "Produktivmodus kann Änderungen an der Veeam-Umgebung durchführen." + $Confirmation = Read-Host "Zum Fortfahren exakt PRODUKTIV eingeben" + + if($Confirmation -cne "PRODUKTIV") { + Write-Host "Produktivlauf wurde abgebrochen." -ForegroundColor Yellow + return + } + + & $ConfigureScript -Apply +} +else { + & $ConfigureScript -DryRun +}