TESTED
This commit is contained in:
@@ -0,0 +1,606 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
|
||||
Prüft alle Veeam Installer Module.
|
||||
|
||||
|
||||
.DESCRIPTION
|
||||
|
||||
- Syntaxprüfung
|
||||
- Importprüfung
|
||||
- Funktionsprüfung
|
||||
- Fehlerreport
|
||||
|
||||
|
||||
.VERSION
|
||||
|
||||
1.0.0
|
||||
|
||||
#>
|
||||
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
|
||||
|
||||
|
||||
############################################################
|
||||
# Pfade
|
||||
############################################################
|
||||
|
||||
|
||||
$RootPath =
|
||||
|
||||
Split-Path `
|
||||
$PSScriptRoot
|
||||
|
||||
|
||||
|
||||
$ModulePath =
|
||||
|
||||
Join-Path `
|
||||
$RootPath `
|
||||
"Modules"
|
||||
|
||||
|
||||
|
||||
$ReportPath =
|
||||
|
||||
Join-Path `
|
||||
$RootPath `
|
||||
"Reports"
|
||||
|
||||
|
||||
|
||||
if(!(Test-Path $ReportPath))
|
||||
{
|
||||
|
||||
New-Item `
|
||||
-Path $ReportPath `
|
||||
-ItemType Directory `
|
||||
-Force |
|
||||
|
||||
Out-Null
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
############################################################
|
||||
# Modul Liste
|
||||
############################################################
|
||||
|
||||
|
||||
$Modules = @(
|
||||
|
||||
"Core\Bootstrap.psm1",
|
||||
|
||||
"Core\Logging.psm1",
|
||||
|
||||
"Core\Configuration.psm1",
|
||||
|
||||
"Core\Utilities.psm1",
|
||||
|
||||
"Core\Security.psm1",
|
||||
|
||||
"Core\SQL.psm1",
|
||||
|
||||
"Core\DryRun.psm1",
|
||||
|
||||
"Veeam\VeeamInstallation.psm1",
|
||||
|
||||
"Veeam\Repository.psm1",
|
||||
|
||||
"Veeam\BackupJobs.psm1"
|
||||
|
||||
)
|
||||
#Beginn PART 2
|
||||
############################################################
|
||||
# Syntaxprüfung
|
||||
############################################################
|
||||
|
||||
function Test-PowerShellSyntax {
|
||||
|
||||
|
||||
param(
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
|
||||
[string]$File
|
||||
|
||||
)
|
||||
|
||||
|
||||
$Result =
|
||||
|
||||
[PSCustomObject]@{
|
||||
|
||||
|
||||
File =
|
||||
|
||||
$File
|
||||
|
||||
|
||||
|
||||
SyntaxOK =
|
||||
|
||||
$false
|
||||
|
||||
|
||||
|
||||
Error =
|
||||
|
||||
$null
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
$Tokens = $null
|
||||
|
||||
$Errors = $null
|
||||
|
||||
|
||||
|
||||
[System.Management.Automation.Language.Parser]
|
||||
::ParseFile(
|
||||
|
||||
$File,
|
||||
|
||||
[ref]$Tokens,
|
||||
|
||||
[ref]$Errors
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
if($Errors.Count -eq 0)
|
||||
{
|
||||
|
||||
$Result.SyntaxOK = $true
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$Result.Error =
|
||||
|
||||
$Errors.Exception.Message
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
|
||||
$Result.Error =
|
||||
|
||||
$_.Exception.Message
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $Result
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
############################################################
|
||||
# Modul Import Prüfung
|
||||
############################################################
|
||||
|
||||
function Test-ModuleImport {
|
||||
|
||||
|
||||
param(
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
|
||||
[string]$File
|
||||
|
||||
)
|
||||
|
||||
|
||||
$ImportedModule = $null
|
||||
|
||||
|
||||
|
||||
$Result =
|
||||
|
||||
[PSCustomObject]@{
|
||||
|
||||
|
||||
File =
|
||||
|
||||
$File
|
||||
|
||||
|
||||
|
||||
ImportOK =
|
||||
|
||||
$false
|
||||
|
||||
|
||||
|
||||
ExportOK =
|
||||
|
||||
$false
|
||||
|
||||
|
||||
|
||||
ExportedFunctions =
|
||||
|
||||
0
|
||||
|
||||
|
||||
|
||||
Error =
|
||||
|
||||
$null
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
$ImportedModule = Import-Module `
|
||||
$File `
|
||||
-Force `
|
||||
-PassThru `
|
||||
-ErrorAction Stop
|
||||
|
||||
|
||||
|
||||
$Result.ImportOK = $true
|
||||
$Result.ExportedFunctions = @($ImportedModule.ExportedFunctions.Keys).Count
|
||||
$Result.ExportOK = $Result.ExportedFunctions -gt 0
|
||||
|
||||
if(!$Result.ExportOK)
|
||||
{
|
||||
$Result.Error = "Das Modul exportiert keine Funktionen."
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
if($null -ne $ImportedModule)
|
||||
{
|
||||
Remove-Module -ModuleInfo $ImportedModule -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
|
||||
$Result.Error =
|
||||
|
||||
$_.Exception.Message
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $Result
|
||||
|
||||
}
|
||||
#Beginn PART 3
|
||||
############################################################
|
||||
# Alle Module prüfen
|
||||
############################################################
|
||||
|
||||
function Invoke-ModuleValidation {
|
||||
|
||||
|
||||
$Results = @()
|
||||
|
||||
|
||||
|
||||
foreach($Module in $Modules)
|
||||
{
|
||||
|
||||
|
||||
$File =
|
||||
|
||||
Join-Path `
|
||||
$ModulePath `
|
||||
$Module
|
||||
|
||||
|
||||
|
||||
if(!(Test-Path $File))
|
||||
{
|
||||
|
||||
$Results += [PSCustomObject]@{
|
||||
|
||||
|
||||
File =
|
||||
|
||||
$File
|
||||
|
||||
|
||||
|
||||
SyntaxOK =
|
||||
|
||||
$false
|
||||
|
||||
|
||||
|
||||
ImportOK =
|
||||
|
||||
$false
|
||||
|
||||
|
||||
|
||||
ExportOK =
|
||||
|
||||
$false
|
||||
|
||||
|
||||
|
||||
ExportedFunctions =
|
||||
|
||||
0
|
||||
|
||||
|
||||
|
||||
Error =
|
||||
|
||||
"Datei nicht gefunden."
|
||||
|
||||
}
|
||||
|
||||
|
||||
continue
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$Syntax =
|
||||
|
||||
Test-PowerShellSyntax `
|
||||
-File $File
|
||||
|
||||
|
||||
|
||||
$Import =
|
||||
|
||||
Test-ModuleImport `
|
||||
-File $File
|
||||
|
||||
|
||||
|
||||
$Results += [PSCustomObject]@{
|
||||
|
||||
|
||||
File =
|
||||
|
||||
$File
|
||||
|
||||
|
||||
|
||||
SyntaxOK =
|
||||
|
||||
$Syntax.SyntaxOK
|
||||
|
||||
|
||||
|
||||
ImportOK =
|
||||
|
||||
$Import.ImportOK
|
||||
|
||||
|
||||
|
||||
ExportOK =
|
||||
|
||||
$Import.ExportOK
|
||||
|
||||
|
||||
|
||||
ExportedFunctions =
|
||||
|
||||
$Import.ExportedFunctions
|
||||
|
||||
|
||||
|
||||
Error =
|
||||
|
||||
if($Syntax.Error)
|
||||
{
|
||||
|
||||
$Syntax.Error
|
||||
|
||||
}
|
||||
elseif($Import.Error)
|
||||
{
|
||||
|
||||
$Import.Error
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$null
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $Results
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
############################################################
|
||||
# Report erstellen
|
||||
############################################################
|
||||
|
||||
function Save-ModuleValidationReport {
|
||||
|
||||
|
||||
param(
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
|
||||
[array]$Results
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
$File =
|
||||
|
||||
Join-Path `
|
||||
$ReportPath `
|
||||
(
|
||||
"ModuleValidation_" +
|
||||
|
||||
(
|
||||
Get-Date `
|
||||
-Format "yyyyMMdd_HHmmss"
|
||||
) +
|
||||
|
||||
".json"
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
$Results |
|
||||
|
||||
ConvertTo-Json `
|
||||
-Depth 5 |
|
||||
Out-File `
|
||||
-FilePath $File `
|
||||
-Encoding UTF8
|
||||
|
||||
|
||||
|
||||
return $File
|
||||
|
||||
}
|
||||
#Beginn PART 4
|
||||
############################################################
|
||||
# Hauptausführung
|
||||
############################################################
|
||||
|
||||
|
||||
Write-Host ""
|
||||
|
||||
Write-Host `
|
||||
"======================================" `
|
||||
-ForegroundColor Cyan
|
||||
|
||||
Write-Host `
|
||||
" Veeam Modul Syntaxprüfung" `
|
||||
-ForegroundColor Cyan
|
||||
|
||||
Write-Host `
|
||||
"======================================" `
|
||||
-ForegroundColor Cyan
|
||||
|
||||
|
||||
|
||||
$ValidationResults =
|
||||
|
||||
Invoke-ModuleValidation
|
||||
|
||||
|
||||
|
||||
$ReportFile =
|
||||
|
||||
Save-ModuleValidationReport `
|
||||
-Results $ValidationResults
|
||||
|
||||
|
||||
|
||||
Write-Host ""
|
||||
|
||||
Write-Host `
|
||||
"Prüfergebnis:" `
|
||||
-ForegroundColor Yellow
|
||||
|
||||
|
||||
|
||||
foreach($Result in $ValidationResults)
|
||||
{
|
||||
|
||||
if(
|
||||
$Result.SyntaxOK -and
|
||||
$Result.ImportOK -and
|
||||
$Result.ExportOK
|
||||
)
|
||||
{
|
||||
|
||||
Write-Host `
|
||||
"[OK] $($Result.File)" `
|
||||
-ForegroundColor Green
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Write-Host `
|
||||
"[FEHLER] $($Result.File)" `
|
||||
-ForegroundColor Red
|
||||
|
||||
|
||||
Write-Host `
|
||||
$Result.Error
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Write-Host ""
|
||||
|
||||
Write-Host `
|
||||
"Report gespeichert:" `
|
||||
-ForegroundColor Cyan
|
||||
|
||||
Write-Host $ReportFile
|
||||
|
||||
|
||||
|
||||
$Failed =
|
||||
|
||||
$ValidationResults |
|
||||
|
||||
Where-Object {
|
||||
|
||||
(!$_.SyntaxOK) -or
|
||||
(!$_.ImportOK) -or
|
||||
(!$_.ExportOK)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if($Failed.Count -gt 0)
|
||||
{
|
||||
|
||||
exit 1
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
exit 0
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user