122 lines
3.6 KiB
PowerShell
122 lines
3.6 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
PS 11: Dateisystem, PSDrives (Env/Cert/Registry) & Hashes
|
|
.DESCRIPTION
|
|
Lerne die einheitliche Verwaltung von Datenquellen über PSProvider:
|
|
- PSDrives (Env:, Cert:, FileSystem)
|
|
- Dateimetadaten, Filterung und Rekursion
|
|
- Kryptografische Integritätsprüfung mit Get-FileHash
|
|
- Temporäres Mounten und Unmounten von PSDrives
|
|
#>
|
|
|
|
# 🎯 TEILZIEL 1 (TODO 1): Get-EnvironmentReport
|
|
<#
|
|
.DESCRIPTION
|
|
Liest Umgebungsvariablen über das `Env:`-PSDrive aus und filtert nach Namen.
|
|
.OUTPUTS
|
|
Array von [PSCustomObject]@{ VariableName = ...; Value = ...; Length = ... }
|
|
#>
|
|
function Get-EnvironmentReport {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter()]
|
|
[string[]]$FilterPatterns = @('*')
|
|
)
|
|
|
|
# TODO:
|
|
# 1. Lies alle Variablen aus dem Env:-Laufwerk aus (Get-ChildItem Env:).
|
|
# 2. Filtere nach den übergebenen $FilterPatterns.
|
|
# 3. Gib für jeden Treffer ein [PSCustomObject] mit VariableName, Value und Value-Länge zurück.
|
|
return @()
|
|
}
|
|
|
|
# 🎯 TEILZIEL 2 (TODO 2): Get-DirectoryFileInventory
|
|
<#
|
|
.DESCRIPTION
|
|
Erstellt ein detailliertes Dateiinventar eines Verzeichnisses inklusive Metadaten und SHA256-Hash.
|
|
.OUTPUTS
|
|
Array von [PSCustomObject]@{ Name, FullPath, Extension, SizeKB, LastModified, SHA256 }
|
|
#>
|
|
function Get-DirectoryFileInventory {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$Path,
|
|
|
|
[Parameter()]
|
|
[string[]]$Extensions = @('*'),
|
|
|
|
[Parameter()]
|
|
[bool]$CalculateHash = $true
|
|
)
|
|
|
|
# TODO:
|
|
# 1. Ermittle alle Dateien unter $Path (rekursiv mit Get-ChildItem -File -Recurse).
|
|
# 2. Filtere nach $Extensions.
|
|
# 3. Berechne SizeKB ([Math]::Round($_.Length / 1KB, 2)).
|
|
# 4. Berechne optional den SHA256-Hash mit Get-FileHash.
|
|
# 5. Gib strukturierte [PSCustomObject]-Einträge zurück.
|
|
return @()
|
|
}
|
|
|
|
# 🎯 TEILZIEL 3 (TODO 3): Test-FileIntegrity
|
|
<#
|
|
.DESCRIPTION
|
|
Prüft die kryptografische Integrität einer Datei anhand eines erwarteten Hashwerts.
|
|
.OUTPUTS
|
|
[PSCustomObject]@{
|
|
FilePath = [string]
|
|
IsValid = [bool]
|
|
CalculatedHash = [string]
|
|
ExpectedHash = [string]
|
|
}
|
|
#>
|
|
function Test-FileIntegrity {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$FilePath,
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$ExpectedHash,
|
|
|
|
[Parameter()]
|
|
[ValidateSet('SHA256', 'SHA384', 'SHA512', 'MD5')]
|
|
[string]$Algorithm = 'SHA256'
|
|
)
|
|
|
|
# TODO:
|
|
# 1. Berechne den Hash der Datei $FilePath mit Get-FileHash -Algorithm $Algorithm.
|
|
# 2. Vergleiche den berechneten Hash mit $ExpectedHash (ohne Beachtung von Groß-/Kleinschreibung).
|
|
# 3. Gib das Status-Objekt zurück.
|
|
return $null
|
|
}
|
|
|
|
# 🎯 TEILZIEL 4 (TODO 4): New-TemporaryMountDrive
|
|
<#
|
|
.DESCRIPTION
|
|
Erstellt ein temporäres PSDrive, führt einen ScriptBlock im Kontext dieses Laufwerks aus
|
|
und entfernt das Laufwerk garantiert wieder (Try/Finally).
|
|
.OUTPUTS
|
|
Das Rückgabe-Ergebnis des ausgeführten ScriptBlocks
|
|
#>
|
|
function New-TemporaryMountDrive {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$DriveName,
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$RootPath,
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
[scriptblock]$Action
|
|
)
|
|
|
|
# TODO:
|
|
# 1. Erstelle das PSDrive mit New-PSDrive -Name $DriveName -PSProvider FileSystem -Root $RootPath.
|
|
# 2. Führe $Action im try-Block aus (& $Action $DriveName).
|
|
# 3. Entferne im finally-Block das Laufwerk mit Remove-PSDrive -Name $DriveName -Force.
|
|
# 4. Gib das Ergebnis zurück.
|
|
return $null
|
|
}
|