scripts/AD_USER_CREATE.ps1 aktualisiert

This commit is contained in:
2024-11-08 08:52:41 +01:00
parent 28169e10dd
commit f5c011000b

View File

@@ -6,7 +6,6 @@ if (-not $myPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::A
Start-Process powershell -ArgumentList $arguments -Verb runAs
Exit
}
Add-Type -AssemblyName System.Windows.Forms
Import-Module ActiveDirectory
@@ -178,13 +177,15 @@ $okButton.Add_Click({
$password = $masterPassword
}
$groupOU = $masterGroupOUComboBox.SelectedItem
# Überprüfen, ob notwendige Felder ausgefüllt sind
if (-not $firstName -or -not $lastName) {
$missingField = if (-not $firstName) { "Vorname" } elseif (-not $lastName) { "Nachname" }
$outputTextBox.AppendText("Fehler bei der Erstellung des Benutzers '$firstName $lastName': $missingField fehlt.`r`n")
continue
}
# Benutzername generieren: erster Buchstabe des Vornamens + Nachname
$username = ($firstName.Substring(0, 1) + $lastName).ToLower()
@@ -199,14 +200,14 @@ $okButton.Add_Click({
# Benutzer erstellen, falls er nicht existiert
try {
New-ADUser -Name "$firstName $lastName" `
-GivenName "$firstName" `
-Surname "$lastName" `
-SamAccountName "$username" `
-UserPrincipalName "$email" `
-AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) `
-Enabled $true `
-Path "$ou"
-GivenName "$firstName" `
-Surname "$lastName" `
-SamAccountName "$username" `
-UserPrincipalName "$email" `
-AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) `
-Enabled $true `
-Path "$ou"
$outputTextBox.SelectionColor = 'Green'
$outputTextBox.AppendText("Benutzer $username wurde erfolgreich erstellt.`r`n")
}
@@ -218,16 +219,17 @@ $okButton.Add_Click({
}
else {
# Erfolgsnachricht für vorhandenen Benutzer
$outputTextBox.SelectionColor = 'Green'
$outputTextBox.SelectionColor = 'RED'
$outputTextBox.AppendText("Benutzer $username existiert bereits.`r`n")
$outputTextBox.AppendText("Fehlerdetails: $_`r`n")
}
# Gruppenzuordnung durchführen, auch wenn der Benutzer schon existiert
if ($globalGroup) {
# Gruppen-OU immer auf die Master-OU setzen
$groupOU = $masterGroupOUComboBox.SelectedItem
try {
# Überprüfen, ob die Gruppe existiert
$groupOU = $masterGroupOUComboBox.SelectedItem
$group = Get-ADGroup -Filter { Name -eq $globalGroup }
try {
# Gruppe erstellen, falls sie nicht existiert
@@ -236,7 +238,7 @@ $okButton.Add_Click({
-GroupScope Global `
-Path $groupOU `
-Description "Globale Gruppe für $groupName"
# Erfolgsnachricht
$outputTextBox.SelectionColor = 'Green'
$outputTextBox.AppendText("Globale Gruppe '$groupName' wurde erfolgreich erstellt.`r`n")
@@ -275,7 +277,7 @@ function Get-EmailAddress {
param (
[string]$username
)
return "$username@$domain"
}