| .. | ||
| aufgabe.ps1 | ||
| index.html | ||
| musterloesung.ps1 | ||
| README.md | ||
| test_aufgabe.ps1 | ||
PS 07: Eigene Cmdlets [CmdletBinding()] & Validierung ⚡
Willkommen zu Modul 07 des PowerShell 7+ Core Kurses!
In diesem Modul entwickelst du echte Enterprise-fähige Cmdlets (Advanced Functions). Mit [CmdletBinding()] rüstest du Skripte mit professionellen Features wie -WhatIf, -Confirm, Parameter-Sets und Validierungs-Attributen aus.
💡 1. Das Wichtigste auf einen Blick
[CmdletBinding()] & Parameter-Attribute
function Get-ServerConfig {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateSet('Development', 'Staging', 'Production')]
[string]$Environment,
[Parameter(Mandatory = $false)]
[ValidateRange(1, 65535)]
[int]$Port = 8080
)
# Logik...
}
-WhatIf und $PSCmdlet.ShouldProcess
function Stop-Server {
[CmdletBinding(SupportsShouldProcess = $true)]
param([Parameter(Mandatory=$true)] [string]$ServerName)
if ($PSCmdlet.ShouldProcess($ServerName, "Server stoppen")) {
# Echte Ausführung...
}
}
🎯 Aufgabenübersicht in aufgabe.ps1
- Get-EnvironmentConfig: Konfigurations-Cmdlet mit
[ValidateSet()]und[ValidateRange()]. - Restart-AppService: Destruktives Cmdlet mit
SupportsShouldProcessund$PSCmdlet.ShouldProcess. - New-UserAccount: Pipeline-fähiges Cmdlet mit Regex-Email-Validierung
[ValidatePattern()]. - Get-SystemReport: Report-Cmdlet mit Switch-Parameter
[switch]$Detailed.