<# --------------------------------------------------------------------------- Module......: Repository.psm1 Projekt.....: Veeam Enterprise Installer Version.....: 2.0.0 Beschreibung: Verwaltung von Veeam Backup Repositories. Funktionen: - Repository Pfade prüfen - Speicher prüfen - Ordner erstellen - Berechtigungen vorbereiten - Repository Konfiguration ---------------------------------------------------------------------------#> Set-StrictMode -Version Latest #region Variablen $Script:RepositoryVersion = "2.0.0" #endregion ############################################################ # Test-RepositoryPath ############################################################ function Test-RepositoryPath { <# .SYNOPSIS Prüft Repository Pfad. #> param( [Parameter(Mandatory)] [string]$Path ) return ( Test-Path $Path ) } ############################################################ # New-RepositoryFolder ############################################################ function New-RepositoryFolder { <# .SYNOPSIS Erstellt Repository Ordner. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { New-Item ` -Path $Path ` -ItemType Directory ` -Force | Out-Null } return $Path } ############################################################ # Get-RepositoryInformation ############################################################ function Get-RepositoryInformation { <# .SYNOPSIS Liefert Repository Informationen. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { return $null } $Drive = Split-Path ` $Path ` -Qualifier $Disk = Get-CimInstance ` Win32_LogicalDisk ` -Filter "DeviceID='$Drive'" [PSCustomObject]@{ Path = $Path SizeGB = [math]::Round( $Disk.Size / 1GB, 2 ) FreeGB = [math]::Round( $Disk.FreeSpace / 1GB, 2 ) Time = Get-Date } } #Beginn PART 2 ############################################################ # Test-RepositoryFreeSpace ############################################################ function Test-RepositoryFreeSpace { <# .SYNOPSIS Prüft freien Speicher. #> param( [Parameter(Mandatory)] [string]$Path, [int]$MinimumGB = 50 ) $Info = Get-RepositoryInformation ` -Path $Path if($null -eq $Info) { return $false } return ( $Info.FreeGB -ge $MinimumGB ) } ############################################################ # Get-RepositoryConfiguration ############################################################ function Get-RepositoryConfiguration { <# .SYNOPSIS Erstellt Repository Konfiguration. #> param( [Parameter(Mandatory)] [string]$Name, [Parameter(Mandatory)] [string]$Path, [int]$RetentionDays = 14 ) [PSCustomObject]@{ Name = $Name Path = $Path RetentionDays = $RetentionDays Type = "Local Repository" Enabled = $true Created = Get-Date } } ############################################################ # Test-RepositoryAccess ############################################################ function Test-RepositoryAccess { <# .SYNOPSIS Prüft Zugriff auf Repository. #> param( [Parameter(Mandatory)] [string]$Path ) try { $TestFile = Join-Path ` $Path ` "repository_test.tmp" New-Item ` -Path $TestFile ` -ItemType File ` -Force | Out-Null Remove-Item ` $TestFile ` -Force return $true } catch { return $false } } ############################################################ # Set-RepositoryPermission ############################################################ function Set-RepositoryPermission { <# .SYNOPSIS Setzt Repository Rechte. #> param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Account ) $ACL = Get-Acl ` -Path $Path $Rule = New-Object ` System.Security.AccessControl.FileSystemAccessRule( $Account, "Modify", "ContainerInherit,ObjectInherit", "None", "Allow" ) $ACL.AddAccessRule( $Rule ) Set-Acl ` -Path $Path ` -AclObject $ACL } #Beginn PART 3 ############################################################ # Test-RepositoryHealth ############################################################ function Test-RepositoryHealth { <# .SYNOPSIS Prüft Repository Zustand. #> param( [Parameter(Mandatory)] [string]$Path ) $Exists = Test-RepositoryPath ` -Path $Path $Access = if($Exists) { Test-RepositoryAccess ` -Path $Path } else { $false } $Space = if($Exists) { Test-RepositoryFreeSpace ` -Path $Path } else { $false } [PSCustomObject]@{ Path = $Path Exists = $Exists Access = $Access FreeSpaceOK = $Space Status = if( $Exists -and $Access -and $Space ) { "OK" } else { "ERROR" } } } ############################################################ # Initialize-Repository ############################################################ function Initialize-Repository { <# .SYNOPSIS Bereitet Repository vor. #> param( [Parameter(Mandatory)] [string]$Path, [string]$Account ) New-RepositoryFolder ` -Path $Path if( $Account ) { Set-RepositoryPermission ` -Path $Path ` -Account $Account } return ( Get-RepositoryInformation ` -Path $Path ) } ############################################################ # Get-RepositoryReport ############################################################ function Get-RepositoryReport { <# .SYNOPSIS Erstellt Repository Bericht. #> param( [Parameter(Mandatory)] [string]$Path ) [PSCustomObject]@{ Repository = $Path Health = Test-RepositoryHealth ` -Path $Path Information = Get-RepositoryInformation ` -Path $Path Time = Get-Date } } #Beginn PART 4 ############################################################ # New-RepositoryDefinitionFile ############################################################ function New-RepositoryDefinitionFile { <# .SYNOPSIS Speichert Repository Konfiguration. #> param( [Parameter(Mandatory)] [PSCustomObject]$Configuration, [string]$Path = "C:\ProgramData\VeeamInstaller\Repository" ) if(!(Test-Path $Path)) { New-Item ` -Path $Path ` -ItemType Directory ` -Force | Out-Null } $File = Join-Path ` $Path ` ( $Configuration.Name + ".json" ) $Configuration | ConvertTo-Json ` -Depth 5 | Out-File ` -FilePath $File ` -Encoding UTF8 return $File } ############################################################ # Get-RepositoryDefinitions ############################################################ function Get-RepositoryDefinitions { <# .SYNOPSIS Liest Repository Definitionen. #> param( [string]$Path = "C:\ProgramData\VeeamInstaller\Repository" ) if(!(Test-Path $Path)) { return $null } Get-ChildItem ` -Path $Path ` -Filter "*.json" | ForEach-Object { Get-Content $_.FullName | ConvertFrom-Json } } ############################################################ # Test-RepositoryConfiguration ############################################################ function Test-RepositoryConfiguration { <# .SYNOPSIS Prüft Repository Konfiguration. #> param( [Parameter(Mandatory)] [PSCustomObject]$Configuration ) [PSCustomObject]@{ Name = $Configuration.Name PathValid = Test-RepositoryPath ` -Path $Configuration.Path RetentionValid = ( $Configuration.RetentionDays -gt 0 ) Status = "Checked" } } ############################################################ # Get-RepositoryPerformanceSettings ############################################################ function Get-RepositoryPerformanceSettings { <# .SYNOPSIS Repository Performance Optionen. #> [PSCustomObject]@{ ConcurrentTasks = 4 StorageOptimization = "LocalTarget" Compression = "Optimal" Deduplication = $true } } #Beginn PART 5 ############################################################ # Test-ImmutablePreparation ############################################################ function Test-ImmutablePreparation { <# .SYNOPSIS Prüft Voraussetzungen für Immutable Storage. Hinweis: Die eigentliche Linux Hardened Repository Konfiguration erfolgt außerhalb dieses Moduls. #> param( [Parameter(Mandatory)] [string]$Path ) [PSCustomObject]@{ Path = $Path DirectoryExists = Test-RepositoryPath ` -Path $Path NTFSProtection = $true ImmutableReady = $false Note = "Externe Storage-Konfiguration erforderlich." } } ############################################################ # Remove-RepositoryTemporaryFiles ############################################################ function Remove-RepositoryTemporaryFiles { <# .SYNOPSIS Entfernt temporäre Dateien. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { return $false } Get-ChildItem ` -Path $Path ` -Filter "*.tmp" ` -Recurse ` -ErrorAction SilentlyContinue | Remove-Item ` -Force ` -ErrorAction SilentlyContinue return $true } ############################################################ # Get-RepositoryStorageAnalysis ############################################################ function Get-RepositoryStorageAnalysis { <# .SYNOPSIS Analysiert Storage. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { return $null } $Files = Get-ChildItem ` -Path $Path ` -File ` -Recurse ` -ErrorAction SilentlyContinue [PSCustomObject]@{ Path = $Path FileCount = $Files.Count SizeGB = [math]::Round( ( $Files | Measure-Object ` -Property Length ` -Sum ).Sum / 1GB, 2 ) Time = Get-Date } } ############################################################ # Repair-RepositoryPermissions ############################################################ function Repair-RepositoryPermissions { <# .SYNOPSIS Repariert Berechtigungen. #> param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Account ) Set-RepositoryPermission ` -Path $Path ` -Account $Account return $true } #Beginn PART 6 ############################################################ # Deploy-RepositoryConfiguration ############################################################ function Deploy-RepositoryConfiguration { <# .SYNOPSIS Bereitet Repository Konfiguration vor. #> param( [Parameter(Mandatory)] [string]$Name, [Parameter(Mandatory)] [string]$Path, [int]$RetentionDays = 14, [string]$Account ) $Configuration = Get-RepositoryConfiguration ` -Name $Name ` -Path $Path ` -RetentionDays $RetentionDays Initialize-Repository ` -Path $Path ` -Account $Account New-RepositoryDefinitionFile ` -Configuration $Configuration return $Configuration } ############################################################ # Test-CompleteRepositoryConfiguration ############################################################ function Test-CompleteRepositoryConfiguration { <# .SYNOPSIS Vollständige Repository Prüfung. #> param( [Parameter(Mandatory)] [string]$Path ) [PSCustomObject]@{ Health = Test-RepositoryHealth ` -Path $Path Storage = Get-RepositoryStorageAnalysis ` -Path $Path Access = Test-RepositoryAccess ` -Path $Path Performance = Get-RepositoryPerformanceSettings Time = Get-Date } } ############################################################ # Get-RepositoryModuleInformation ############################################################ function Get-RepositoryModuleInformation { <# .SYNOPSIS Liefert Modulinformationen. #> [PSCustomObject]@{ Module = "Repository.psm1" Version = $Script:RepositoryVersion Loaded = $true } } #Beginn PART 7 ############################################################ # Export Module Functions ############################################################ Export-ModuleMember ` -Function ` Test-RepositoryPath, New-RepositoryFolder, Get-RepositoryInformation, Test-RepositoryFreeSpace, Get-RepositoryConfiguration, Test-RepositoryAccess, Set-RepositoryPermission, Test-RepositoryHealth, Initialize-Repository, Get-RepositoryReport, New-RepositoryDefinitionFile, Get-RepositoryDefinitions, Test-RepositoryConfiguration, Get-RepositoryPerformanceSettings, Test-ImmutablePreparation, Remove-RepositoryTemporaryFiles, Get-RepositoryStorageAnalysis, Repair-RepositoryPermissions, Deploy-RepositoryConfiguration, Test-CompleteRepositoryConfiguration, Get-RepositoryModuleInformation ############################################################ # Ende Repository.psm1 ############################################################