120 lines
4.3 KiB
PowerShell
120 lines
4.3 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Entfernt das H-Laufwerk (Home-Drive) bei Benutzern in bestimmten OUs
|
|
und aktualisiert das Beschreibungsfeld inklusive Logging.
|
|
.DESCRIPTION
|
|
Das Skript durchläuft ein Array von OUs, sucht alle aktiven Benutzer,
|
|
entfernt die Attribute 'HomeDirectory' und 'HomeDrive' (H:) und
|
|
ergänzt das 'Description'-Feld um einen Zeitstempel.
|
|
#>
|
|
|
|
# --- KONFIGURATION ---
|
|
# Hier die OUs eintragen. Am sichersten sind vollständige Distinguished Names (DN).
|
|
# Beispiel für Kurznamen/Suchbegriffe siehe Filter unten im Skript.
|
|
$OUList = @(
|
|
"OU=NNG,OU=Users,OU=N-Tree,DC=n-ad,DC=n-ergie, OU=NSG,OU=Users,OU=N-Tree,DC=n-ad,DC=n-ergie"
|
|
)
|
|
|
|
# Pfad für das Logfile
|
|
$LogPath = "C:\Temp\H_Laufwerk\Logs"
|
|
$LogFile = Join-Path $LogPath "H-Laufwerk_Entfernung_$(Get-Date -Format 'yyyy-MM-dd').log"
|
|
|
|
# SICHERHEITS-SCHALTER (Safety First)
|
|
# $true = Es wird nur simuliert und geloggt, was passieren WÜRDE.
|
|
# $false = Die Änderungen werden direkt ins Active Directory geschrieben!
|
|
$WhatIfMode = $true
|
|
|
|
# --- FUNKTIONEN ---
|
|
Function Write-Log {
|
|
Param (
|
|
[string]$Message,
|
|
[ValidateSet("INFO", "SUCCESS", "WARNING", "ERROR")]
|
|
[string]$Type = "INFO"
|
|
)
|
|
$TimeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
$LogLine = "[$TimeStamp] [$Type] $Message"
|
|
|
|
# Konsole-Ausgabe (farblich angepasst)
|
|
switch ($Type) {
|
|
"SUCCESS" { Write-Host $LogLine -ForegroundColor Green }
|
|
"WARNING" { Write-Host $LogLine -ForegroundColor Yellow }
|
|
"ERROR" { Write-Host $LogLine -ForegroundColor Red }
|
|
default { Write-Host $LogLine -ForegroundColor Cyan }
|
|
}
|
|
|
|
# In Datei schreiben
|
|
$LogLine | Out-File -FilePath $LogFile -Append -Encoding utf8
|
|
}
|
|
|
|
# --- INITIALISIERUNG ---
|
|
if (-not (Test-Path $LogPath)) {
|
|
New-Item -ItemType Directory -Path $LogPath -Force | Out-Null
|
|
}
|
|
|
|
Write-Log "=== Skriptstart (WhatIf-Modus = $WhatIfMode) ===" "INFO"
|
|
|
|
# AD-Modul prüfen
|
|
if (-not (Get-Module -ListAvailable -Name ActiveDirectory)) {
|
|
Write-Log "Das ActiveDirectory-Modul ist nicht installiert!" "ERROR"
|
|
Exit
|
|
}
|
|
Import-Module ActiveDirectory
|
|
|
|
# Das aktuelle Datum für die Beschreibung generieren
|
|
$CurrentDate = Get-Date -Format "dd.MM.yyyy"
|
|
$AppendText = "H Laufwerk wurde entfernt Datum $CurrentDate"
|
|
|
|
# --- HAUPTTEIL ---
|
|
foreach ($OU in $OUList) {
|
|
Write-Log "Verarbeite OU: $OU" "INFO"
|
|
|
|
try {
|
|
# Alle Benutzer aus der aktuellen OU abrufen, die ein HomeDirectory gesetzt haben
|
|
$Users = Get-ADUser -SearchBase $OU -SearchScope Subtree -Filter * -Properties Description, HomeDirectory, HomeDrive
|
|
}
|
|
catch {
|
|
Write-Log "Fehler beim Lesen der OU '$OU': $_" "ERROR"
|
|
continue
|
|
}
|
|
|
|
if ($null -eq $Users -or $Users.Count -eq 0) {
|
|
Write-Log "Keine Benutzer in der OU '$OU' gefunden." "WARNING"
|
|
continue
|
|
}
|
|
|
|
foreach ($User in $Users) {
|
|
# Prüfen, ob überhaupt ein H-Laufwerk oder HomeDirectory vorhanden ist
|
|
if ([string]::IsNullOrEmpty($User.HomeDirectory) -and [string]::IsNullOrEmpty($User.HomeDrive)) {
|
|
Write-Log "Benutzer $($User.SamAccountName): Kein H-Laufwerk konfiguriert. Überspringe." "INFO"
|
|
continue
|
|
}
|
|
|
|
# Neue Beschreibung zusammensetzen (bestehende Beschreibung beibehalten)
|
|
$NewDescription = if ([string]::IsNullOrEmpty($User.Description)) {
|
|
$AppendText
|
|
} else {
|
|
"$($User.Description) | $AppendText"
|
|
}
|
|
|
|
# Log-Eintrag für die Aktion vorbereiten
|
|
$ActionMsg = "Benutzer $($User.SamAccountName) - Entferne Pfad: '$($User.HomeDirectory)' ($($User.HomeDrive)) | Neue Beschreibung: '$NewDescription'"
|
|
|
|
try {
|
|
if ($WhatIfMode) {
|
|
Write-Log "[WHAT-IF] $ActionMsg" "INFO"
|
|
} else {
|
|
# Änderungen scharf durchführen
|
|
Set-ADUser -Identity $User.SamAccountName `
|
|
-Clear "HomeDirectory", "HomeDrive" `
|
|
-Description $NewDescription
|
|
|
|
Write-Log "Erfolgreich angepasst: $($User.SamAccountName)" "SUCCESS"
|
|
}
|
|
}
|
|
catch {
|
|
Write-Log "Fehler beim Verarbeiten von $($User.SamAccountName): $_" "ERROR"
|
|
}
|
|
}
|
|
}
|
|
|
|
Write-Log "=== Skript beendet ===" "INFO" |