scripts/AD_USER_CREATE.ps1 aktualisiert

This commit is contained in:
2024-11-07 11:14:38 +01:00
parent 76cdc9e6d2
commit b69041f1f4

View File

@@ -74,15 +74,17 @@ $dataGridView.Columns[0].Name = "Titel" # Titel in der ersten Spalte
$dataGridView.Columns[1].Name = "Vorname" # Vorname
$dataGridView.Columns[2].Name = "Nachname" # Nachname
$dataGridView.Columns[3].Name = "Globalgruppe" # Globalgruppe
$dataGridView.Columns[4].Name = "OU" # OU (wird als ComboBox hinzugefügt)
$dataGridView.Columns[5].Name = "Standardpasswort" # Standardpasswort
$dataGridView.Columns[4].Name = "Standardpasswort" # Standardpasswort
$dataGridView.Columns[5].Name = "OU" # OU (wird als ComboBox hinzugefügt)
$dataGridView.Columns[5].Width = 220
# Dropdown für die OU in der DataGridView-ComboBox-Spalte
$ouComboBoxColumn = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
$ouComboBoxColumn.HeaderText = "OU"
$ouComboBoxColumn.Items.AddRange($ouList) # Hier fügen wir alle OUs hinzu
$dataGridView.Columns.RemoveAt(4) # Entfernt die ursprüngliche OU-Spalte
$dataGridView.Columns.Insert(4, $ouComboBoxColumn) # Fügt die ComboBox-Spalte an der richtigen Stelle ein
$dataGridView.Columns.RemoveAt(5) # Entfernt die ursprüngliche OU-Spalte
$dataGridView.Columns.Insert(5, $ouComboBoxColumn) # Fügt die ComboBox-Spalte an der richtigen Stelle ein
$ouComboBoxColumn.Width = 220 # Du kannst hier den Wert nach Bedarf anpassen
# Funktion zum Setzen der OUs für alle Zeilen im DataGridView
function Update-OUsInGrid {
@@ -90,7 +92,7 @@ function Update-OUsInGrid {
foreach ($row in $dataGridView.Rows) {
if ($row.Index -lt $dataGridView.RowCount - 1) { # Nicht für die leere letzte Zeile
# Setze die Master-OU in der "OU"-Spalte für jede Zeile
$row.Cells[4].Value = $selectedMasterOU
$row.Cells[5].Value = $selectedMasterOU # Zeile aktualisieren, statt der 4. Spalte jetzt die 5. für OU
}
}
}
@@ -111,7 +113,7 @@ $form.Controls.Add($outputTextBox)
# OK-Button erstellen
$okButton = New-Object System.Windows.Forms.Button
$okButton.Text = "Benutzer erstellen"
$okButton.Size = New-Object System.Drawing.Size(150, 30) # Größe des Buttons festgelegt
$okButton.Size = New-Object System.Drawing.Size(380, 30) # Größe des Buttons festgelegt
$okButton.Location = New-Object System.Drawing.Point(10, 510) # Position des Buttons unter der TextBox
$okButton.Add_Click({
foreach ($row in $dataGridView.Rows) {
@@ -120,8 +122,8 @@ $okButton.Add_Click({
$firstName = $row.Cells[1].Value
$lastName = $row.Cells[2].Value
$globalGroup = $row.Cells[3].Value
$ou = $row.Cells[4].Value # Hier wird die ausgewählte OU aus der ComboBox abgerufen
$password = $row.Cells[5].Value
$password = $row.Cells[4].Value
$ou = $row.Cells[5].Value # Hier wird die ausgewählte OU aus der ComboBox abgerufen
# Überprüfen, ob notwendige Felder ausgefüllt sind
if (-not $firstName -or -not $lastName) {
@@ -153,14 +155,14 @@ $okButton.Add_Click({
$user = Get-ADUser -Filter { SamAccountName -eq $username }
if (-not $user) {
# Benutzer erstellen, falls er nicht existiert
New-ADUser -Name "$firstName $lastName" `
-GivenName $firstName `
-Surname $lastName `
-SamAccountName $username `
-UserPrincipalName $email `
-EmailAddress $email `
-AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) `
-Enabled $true `
New-ADUser -Name "$firstName $lastName"
-GivenName $firstName
-Surname $lastName
-SamAccountName $username
-UserPrincipalName $email
-EmailAddress $email
-AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force)
-Enabled $true
-Path $ou
# Erfolgsnachricht für Benutzererstellung in grün
@@ -175,16 +177,16 @@ $okButton.Add_Click({
# Gruppenzuordnung durchführen, auch wenn der Benutzer schon existiert
if ($globalGroup) {
# Gruppen-OU auswählen (aus der ComboBox für Gruppen)
$groupOU = $masterGroupOUComboBox.SelectedItem
# Gruppen-OU immer auf die Master-OU setzen
$groupOU = $masterGroupOU
try {
# Überprüfen, ob die Gruppe existiert, und gegebenenfalls erstellen
$group = Get-ADGroup -Filter { Name -eq $globalGroup }
if (-not $group) {
# Gruppe erstellen, falls sie nicht existiert
New-ADGroup -Name $globalGroup `
-GroupScope Global `
-Path $groupOU `
New-ADGroup -Name $globalGroup
-GroupScope Global
-Path $groupOU
-Description "Globale Gruppe für $globalGroup"
# Erfolgsnachricht für Gruppenerstellung
@@ -204,7 +206,7 @@ $okButton.Add_Click({
} catch {
# Fehler bei der Benutzererstellung
$outputTextBox.SelectionColor = 'Red'
$outputTextBox.AppendText("Fehler bei der Erstellung des Benutzers $firstName '$lastName': $_.Exception.Message`r`n")
$outputTextBox.AppendText("Fehler bei der Erstellung des Benutzers '$firstName $lastName': $_.Exception.Message`r`n")
}
}
}
@@ -213,8 +215,8 @@ $okButton.Add_Click({
# Beenden-Button erstellen
$exitButton = New-Object System.Windows.Forms.Button
$exitButton.Text = "Beenden"
$exitButton.Size = New-Object System.Drawing.Size(150, 30) # Größe des Buttons festgelegt
$exitButton.Location = New-Object System.Drawing.Point(624, 510) # Position des Beenden-Buttons
$exitButton.Size = New-Object System.Drawing.Size(380, 30) # Größe des Buttons festgelegt
$exitButton.Location = New-Object System.Drawing.Point(395, 510) # Position des Beenden-Buttons
$exitButton.Add_Click({
$form.Close() # Formular schließen
})