From c3171bd39719c5f6def2f00702daa8797f75ca1e Mon Sep 17 00:00:00 2001 From: AlpSantoGlobalMomentumLLC <128291039+AlpSantoGlobalMomentumLLC@users.noreply.github.com> Date: Mon, 19 May 2025 09:13:56 +0200 Subject: [PATCH 1/2] Add shutdown script with exclusion list --- README.md | 3 ++ ShutdownWithExclusion.ps1 | 65 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 ShutdownWithExclusion.ps1 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..60b6b9d --- /dev/null +++ b/ShutdownWithExclusion.ps1 @@ -0,0 +1,65 @@ +<# +.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 + } catch { + Write-Error "Fehler beim Herunterfahren: $_" + $global:ExitCode = 2 + } + } +} + +Write-Verbose "Skriptausfuehrung beendet. Exit-Code: $global:ExitCode" +exit $global:ExitCode From c2476d1744e7d4edee5b9b7f8e410ce4571e7d95 Mon Sep 17 00:00:00 2001 From: AlpSantoGlobalMomentumLLC <128291039+AlpSantoGlobalMomentumLLC@users.noreply.github.com> Date: Mon, 19 May 2025 09:16:32 +0200 Subject: [PATCH 2/2] Update ShutdownWithExclusion.ps1 --- ShutdownWithExclusion.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ShutdownWithExclusion.ps1 b/ShutdownWithExclusion.ps1 index 60b6b9d..ea875c1 100644 --- a/ShutdownWithExclusion.ps1 +++ b/ShutdownWithExclusion.ps1 @@ -53,7 +53,8 @@ if (-not $isListSyntaxValid) { } else { Write-Host "Der Computer '$CurrentComputerName' wird heruntergefahren." try { - Stop-Computer -Force -ErrorAction Stop + #Stop-Computer -Force -ErrorAction Stop + Write-Host "Shutdown ist auskommentiert, für Testzwecke" } catch { Write-Error "Fehler beim Herunterfahren: $_" $global:ExitCode = 2