diff --git a/README.md b/README.md index c7d2adb..5db9261 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # PersonalPowerShellScripts Here you can find personal, not generalized Powershell scripts, for the daily work + +## New script +- `ShutdownWithExclusion.ps1` - Shut down the local machine unless the hostname appears in an internal ignore list. diff --git a/ShutdownWithExclusion.ps1 b/ShutdownWithExclusion.ps1 new file mode 100644 index 0000000..ea875c1 --- /dev/null +++ b/ShutdownWithExclusion.ps1 @@ -0,0 +1,66 @@ +<# +.SYNOPSIS + Faehrt den Computer herunter, sofern der aktuelle Rechner nicht auf einer vordefinierten Ignorierliste steht. +.DESCRIPTION + Dieses Skript enthaelt intern eine komma-separierte Liste von Rechnernamen, die vom Herunterfahren ausgeschlossen sind. Vor dem Ausfuehren des Shutdowns wird die Liste auf Syntaxfehler geprueft. Ist die Liste fehlerhaft oder enthaelt sie den lokalen Rechnernamen (Groß-/Kleinschreibung wird ignoriert), wird kein Shutdown durchgefuehrt. +#> + +[CmdletBinding()] +param () + +Set-StrictMode -Version Latest +$global:ExitCode = 0 + +# --- Konfiguration --- +$IgnoredComputersRawString = "CLIENT01, SERVER02, DESKTOP05, client99, workstationX" + +# --- Validierung der Ignorierliste --- +$IgnoredComputerNamesList = @() +$isListSyntaxValid = $true + +Write-Verbose "Analysiere die Ignorierliste: '$IgnoredComputersRawString'" + +if ([string]::IsNullOrWhiteSpace($IgnoredComputersRawString)) { + Write-Verbose "Die Ignorierliste ist leer oder besteht nur aus Leerzeichen. Es werden keine Computer ignoriert." +} else { + $splitItems = $IgnoredComputersRawString -split '\s*,\s*' + foreach ($item in $splitItems) { + $trimmedItem = $item.Trim() + if ([string]::IsNullOrEmpty($trimmedItem)) { + Write-Error "Syntaxfehler in der Ignorierliste: Ein Eintrag ist nach der Bereinigung leer." + $isListSyntaxValid = $false + $global:ExitCode = 1 + break + } + $IgnoredComputerNamesList += $trimmedItem + } +} + +# --- Hauptlogik --- +if (-not $isListSyntaxValid) { + Write-Warning "Der Computer wird aufgrund von Fehlern in der Ignorierliste NICHT heruntergefahren." +} else { + $CurrentComputerName = $env:COMPUTERNAME + Write-Verbose "Aktueller Computername: $CurrentComputerName" + if ($IgnoredComputerNamesList.Count -gt 0) { + Write-Verbose "Bereinigte und validierte Ignorierliste: $($IgnoredComputerNamesList -join ', ')" + } else { + Write-Verbose "Die Ignorierliste ist leer. Es werden keine Computer aktiv ignoriert." + } + + if ($IgnoredComputerNamesList -icontains $CurrentComputerName) { + Write-Host "Der Computer '$CurrentComputerName' steht auf der Ignorierliste. Kein Shutdown." + } else { + Write-Host "Der Computer '$CurrentComputerName' wird heruntergefahren." + try { + #Stop-Computer -Force -ErrorAction Stop + Write-Host "Shutdown ist auskommentiert, für Testzwecke" + } catch { + Write-Error "Fehler beim Herunterfahren: $_" + $global:ExitCode = 2 + } + } +} + +Write-Verbose "Skriptausfuehrung beendet. Exit-Code: $global:ExitCode" +exit $global:ExitCode