225 lines
10 KiB
PowerShell
225 lines
10 KiB
PowerShell
Add-Type -AssemblyName System.Windows.Forms
|
|
Import-Module ActiveDirectory
|
|
|
|
# Master-OU und Domäne ermitteln
|
|
$masterOU = "OU=Benutzer,DC=domain,DC=com" # Beispielwert, passe ihn an eure Umgebung an
|
|
$masterGroupOU = "OU=Gruppen,DC=domain,DC=com" # Beispielwert, passe ihn an eure Umgebung an
|
|
|
|
# Holen Sie sich die Domäne aus dem Active Directory
|
|
$domain = (Get-ADDomain).DNSRoot
|
|
|
|
# GUI erstellen
|
|
$form = New-Object System.Windows.Forms.Form
|
|
$form.Text = "Benutzererstellung"
|
|
$form.Size = New-Object System.Drawing.Size(800, 600) # Gesamtgröße der Form anpassen
|
|
|
|
# Master-OU für Benutzer Auswahl
|
|
$masterOULabel = New-Object System.Windows.Forms.Label
|
|
$masterOULabel.Text = "Master-OU (Benutzer):"
|
|
$masterOULabel.Location = New-Object System.Drawing.Point(10, 10)
|
|
$masterOULabel.Size = New-Object System.Drawing.Size(120, 20)
|
|
$form.Controls.Add($masterOULabel)
|
|
|
|
# Master-OU ComboBox für Benutzer
|
|
$masterOUComboBox = New-Object System.Windows.Forms.ComboBox
|
|
$masterOUComboBox.Location = New-Object System.Drawing.Point(130, 10)
|
|
$masterOUComboBox.Size = New-Object System.Drawing.Size(250, 20) # ComboBox-Größe festgelegt
|
|
$masterOUComboBox.DropDownStyle = 'DropDownList'
|
|
|
|
# OUs aus dem AD für Benutzer abrufen und in die ComboBox einfügen
|
|
$ouList = Get-ADOrganizationalUnit -Filter * | Select-Object -ExpandProperty DistinguishedName
|
|
$masterOUComboBox.Items.AddRange($ouList)
|
|
$masterOUComboBox.SelectedItem = $masterOU # Standardwerte setzen
|
|
$form.Controls.Add($masterOUComboBox)
|
|
|
|
# Master-OU für Gruppen Auswahl (Rechts oben)
|
|
$masterGroupOULabel = New-Object System.Windows.Forms.Label
|
|
$masterGroupOULabel.Text = "Master-OU (Gruppen):"
|
|
$masterGroupOULabel.Location = New-Object System.Drawing.Point(400, 10)
|
|
$masterGroupOULabel.Size = New-Object System.Drawing.Size(120, 20)
|
|
$form.Controls.Add($masterGroupOULabel)
|
|
|
|
# Master-OU ComboBox für Gruppen
|
|
$masterGroupOUComboBox = New-Object System.Windows.Forms.ComboBox
|
|
$masterGroupOUComboBox.Location = New-Object System.Drawing.Point(520, 10)
|
|
$masterGroupOUComboBox.Size = New-Object System.Drawing.Size(250, 20) # ComboBox-Größe für Gruppen
|
|
$masterGroupOUComboBox.DropDownStyle = 'DropDownList'
|
|
|
|
# OUs aus dem AD für Gruppen abrufen und in die ComboBox einfügen
|
|
$groupOUList = Get-ADOrganizationalUnit -Filter * | Select-Object -ExpandProperty DistinguishedName
|
|
$masterGroupOUComboBox.Items.AddRange($groupOUList)
|
|
$masterGroupOUComboBox.SelectedItem = $masterGroupOU # Standardwerte setzen
|
|
$form.Controls.Add($masterGroupOUComboBox)
|
|
|
|
# DataGridView erstellen
|
|
$dataGridView = New-Object System.Windows.Forms.DataGridView
|
|
$dataGridView.Size = New-Object System.Drawing.Size(764, 300) # Breite und Höhe des DataGridViews festgelegt
|
|
$dataGridView.Location = New-Object System.Drawing.Point(10, 40) # Position des DataGridViews
|
|
$dataGridView.Anchor = [System.Windows.Forms.AnchorStyles]::Top
|
|
$form.Controls.Add($dataGridView)
|
|
|
|
# Definieren der Spalten für die DataGridView
|
|
$dataGridView.ColumnCount = 6
|
|
$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
|
|
$dataGridView.Columns[5].Name = "Standardpasswort" # Standardpasswort
|
|
|
|
# Funktion zum Abrufen der verfügbaren OUs
|
|
function Get-OUs {
|
|
return Get-ADOrganizationalUnit -Filter * | Select-Object -ExpandProperty DistinguishedName
|
|
}
|
|
|
|
# Funktion zum Setzen der OUs für alle Zeilen im DataGridView
|
|
function Update-OUsInGrid {
|
|
$selectedMasterOU = $masterOUComboBox.SelectedItem
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
# Master-OU ComboBox geändert: Aktualisiere alle OUs im DataGridView
|
|
$masterOUComboBox.Add_SelectedIndexChanged({
|
|
Update-OUsInGrid
|
|
})
|
|
|
|
# RichTextBox für Ausgaben (anstelle von TextBox)
|
|
$outputTextBox = New-Object System.Windows.Forms.RichTextBox
|
|
$outputTextBox.Multiline = $true
|
|
$outputTextBox.Location = New-Object System.Drawing.Point(10, 350) # Position unterhalb des DataGridViews
|
|
$outputTextBox.Size = New-Object System.Drawing.Size(764, 150) # Größe der RichTextBox festgelegt
|
|
$outputTextBox.ScrollBars = 'Vertical'
|
|
$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.Location = New-Object System.Drawing.Point(10, 510) # Position des Buttons unter der TextBox
|
|
$okButton.Add_Click({
|
|
foreach ($row in $dataGridView.Rows) {
|
|
if ($row.Index -lt $dataGridView.RowCount - 1) { # Nicht für die leere letzte Zeile
|
|
$title = $row.Cells[0].Value
|
|
$firstName = $row.Cells[1].Value
|
|
$lastName = $row.Cells[2].Value
|
|
$globalGroup = $row.Cells[3].Value
|
|
$ou = $row.Cells[4].Value # Nun enthält $ou den DistinguishedName der OU
|
|
$password = $row.Cells[5].Value
|
|
|
|
# Ü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()
|
|
|
|
# Passwort prüfen
|
|
if (-not $password) {
|
|
$outputTextBox.AppendText("Fehler bei der Erstellung des Benutzers '$firstName $lastName': Passwort fehlt.`r`n")
|
|
continue
|
|
}
|
|
|
|
# E-Mail-Adresse generieren
|
|
$email = Get-EmailAddress -firstName $firstName -lastName $lastName
|
|
|
|
# Master-OU für den Benutzer setzen, falls keine OU explizit ausgewählt wurde
|
|
if (-not $ou) {
|
|
$ou = $masterOUComboBox.SelectedItem
|
|
}
|
|
|
|
# Benutzer erstellen
|
|
try {
|
|
# Versuchen, den Benutzer abzurufen (auch wenn er bereits existiert)
|
|
$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 `
|
|
-Path $ou
|
|
|
|
# Erfolgsnachricht für Benutzererstellung in grün
|
|
$outputTextBox.SelectionColor = 'Green'
|
|
$outputTextBox.AppendText("Benutzer $username wurde erfolgreich erstellt.`r`n")
|
|
}
|
|
else {
|
|
# Erfolgsnachricht für vorhandenen Benutzer in grün
|
|
$outputTextBox.SelectionColor = 'Green'
|
|
$outputTextBox.AppendText("Benutzer $username existiert bereits.`r`n")
|
|
}
|
|
|
|
# Gruppenzuordnung durchführen, auch wenn der Benutzer schon existiert
|
|
if ($globalGroup) {
|
|
# Gruppen-OU auswählen (aus der ComboBox für Gruppen)
|
|
$groupOU = $masterGroupOUComboBox.SelectedItem
|
|
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 `
|
|
-Description "Globale Gruppe für $globalGroup"
|
|
|
|
# Erfolgsnachricht für Gruppenerstellung
|
|
$outputTextBox.SelectionColor = 'Green'
|
|
$outputTextBox.AppendText("Globale Gruppe '$globalGroup' wurde erfolgreich erstellt.`r`n")
|
|
}
|
|
# Benutzer der Gruppe zuordnen
|
|
Add-ADGroupMember -Identity $globalGroup -Members $username
|
|
$outputTextBox.SelectionColor = 'Green'
|
|
$outputTextBox.AppendText("Benutzer '$username' wurde erfolgreich der Gruppe '$globalGroup' zugeordnet.`r`n")
|
|
} catch {
|
|
# Fehler bei der Gruppenzuordnung in rot
|
|
$outputTextBox.SelectionColor = 'Red'
|
|
$outputTextBox.AppendText("Fehler bei der Gruppenzuordnung für Benutzer '$username': $_.Exception.Message`r`n")
|
|
}
|
|
}
|
|
} catch {
|
|
# Fehler bei der Benutzererstellung
|
|
$outputTextBox.SelectionColor = 'Red'
|
|
$outputTextBox.AppendText("Fehler bei der Erstellung des Benutzers $firstName '$lastName': $_.Exception.Message`r`n")
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
# 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.Add_Click({
|
|
$form.Close() # Formular schließen
|
|
})
|
|
|
|
# Buttons zum Formular hinzufügen
|
|
$form.Controls.Add($okButton)
|
|
$form.Controls.Add($exitButton)
|
|
|
|
# Funktion zum Erstellen der E-Mail-Adresse
|
|
function Get-EmailAddress {
|
|
param (
|
|
[string]$firstName,
|
|
[string]$lastName
|
|
)
|
|
return "$firstName.$lastName@$domain"
|
|
}
|
|
|
|
# Formular anzeigen
|
|
$form.ShowDialog()
|