scripts/AD_GROUPS_CREATE.ps1 aktualisiert
This commit is contained in:
@@ -1,169 +1,278 @@
|
|||||||
$runAsAdmin = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
|
$myWindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
||||||
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
|
$myPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsIdentity)
|
||||||
|
|
||||||
if (-not $runAsAdmin.IsInRole($adminRole)) {
|
if (-not $myPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||||||
# Relaunch the script as Administrator
|
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
|
||||||
$arguments = "$($myinvocation.MyCommand.Definition)"
|
Start-Process powershell -ArgumentList $arguments -Verb runAs
|
||||||
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File $arguments" -Verb RunAs
|
Exit
|
||||||
exit
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Add-Type -AssemblyName System.Windows.Forms
|
Add-Type -AssemblyName System.Windows.Forms
|
||||||
Import-Module ActiveDirectory
|
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
|
# Holen Sie sich die Domäne aus dem Active Directory
|
||||||
$domain = (Get-ADDomain).DNSRoot
|
$domain = (Get-ADDomain).DNSRoot
|
||||||
|
|
||||||
# GUI erstellen
|
# GUI erstellen
|
||||||
$form = New-Object System.Windows.Forms.Form
|
$form = New-Object System.Windows.Forms.Form
|
||||||
$form.Text = "Gruppen Erstellung"
|
$form.Text = "Benutzererstellung"
|
||||||
$form.Size = New-Object System.Drawing.Size(600, 555) # Gesamtgröße der Form anpassen
|
$form.Size = New-Object System.Drawing.Size(990, 600) # Gesamtgröße der Form anpassen
|
||||||
|
|
||||||
# DataGridView für Gruppen erstellen
|
# Funktion zur Berechnung der maximalen Breite der ComboBox
|
||||||
|
function Set-DropDownWidth {
|
||||||
|
param (
|
||||||
|
[System.Windows.Forms.ComboBox]$comboBox
|
||||||
|
)
|
||||||
|
|
||||||
|
$maxLength = 0
|
||||||
|
foreach ($item in $comboBox.Items) {
|
||||||
|
$itemLength = $item.Length
|
||||||
|
if ($itemLength -gt $maxLength) {
|
||||||
|
$maxLength = $itemLength
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Setze die DropDownWidth entsprechend der maximalen Länge
|
||||||
|
$comboBox.DropDownWidth = $maxLength * 6 # Schätzbreite (Zeichen * 8 Pixel)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Masterkennwort Eingabefeld
|
||||||
|
$masterPasswordLabel = New-Object System.Windows.Forms.Label
|
||||||
|
$masterPasswordLabel.Text = "Masterkennwort:"
|
||||||
|
$masterPasswordLabel.Location = New-Object System.Drawing.Point(10, 12)
|
||||||
|
$masterPasswordLabel.Size = New-Object System.Drawing.Size(100, 20)
|
||||||
|
$form.Controls.Add($masterPasswordLabel)
|
||||||
|
|
||||||
|
$masterPasswordTextBox = New-Object System.Windows.Forms.TextBox
|
||||||
|
$masterPasswordTextBox.Location = New-Object System.Drawing.Point(110, 10)
|
||||||
|
$masterPasswordTextBox.Size = New-Object System.Drawing.Size(200, 20)
|
||||||
|
$masterPasswordTextBox.UseSystemPasswordChar = $true # Passwortfeld
|
||||||
|
$form.Controls.Add($masterPasswordTextBox)
|
||||||
|
|
||||||
|
# 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(320, 12)
|
||||||
|
$masterOULabel.Size = New-Object System.Drawing.Size(120, 20)
|
||||||
|
$form.Controls.Add($masterOULabel)
|
||||||
|
|
||||||
|
$masterOUComboBox = New-Object System.Windows.Forms.ComboBox
|
||||||
|
$masterOUComboBox.Location = New-Object System.Drawing.Point(440, 10)
|
||||||
|
$masterOUComboBox.Size = New-Object System.Drawing.Size(200, 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)
|
||||||
|
|
||||||
|
# Berechne und setze die DropDownWidth basierend auf der maximalen Länge
|
||||||
|
Set-DropDownWidth -comboBox $masterOUComboBox
|
||||||
|
|
||||||
|
# Master-OU für Gruppen Auswahl
|
||||||
|
$masterGroupOULabel = New-Object System.Windows.Forms.Label
|
||||||
|
$masterGroupOULabel.Text = "Master-OU (Gruppen):"
|
||||||
|
$masterGroupOULabel.Location = New-Object System.Drawing.Point(645, 12)
|
||||||
|
$masterGroupOULabel.Size = New-Object System.Drawing.Size(120, 20)
|
||||||
|
$form.Controls.Add($masterGroupOULabel)
|
||||||
|
|
||||||
|
$masterGroupOUComboBox = New-Object System.Windows.Forms.ComboBox
|
||||||
|
$masterGroupOUComboBox.Location = New-Object System.Drawing.Point(765, 10)
|
||||||
|
$masterGroupOUComboBox.Size = New-Object System.Drawing.Size(200, 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)
|
||||||
|
|
||||||
|
# Berechne und setze die DropDownWidth basierend auf der maximalen Länge
|
||||||
|
Set-DropDownWidth -comboBox $masterGroupOUComboBox
|
||||||
|
|
||||||
|
# DataGridView erstellen
|
||||||
$dataGridView = New-Object System.Windows.Forms.DataGridView
|
$dataGridView = New-Object System.Windows.Forms.DataGridView
|
||||||
$dataGridView.Size = New-Object System.Drawing.Size(564, 300) # Breite und Höhe des DataGridViews
|
$dataGridView.Size = New-Object System.Drawing.Size(954, 300) # Breite und Höhe des DataGridViews festgelegt
|
||||||
$dataGridView.Location = New-Object System.Drawing.Point(10, 10) # Position des DataGridViews
|
$dataGridView.Location = New-Object System.Drawing.Point(10, 40) # Position des DataGridViews
|
||||||
|
$dataGridView.Anchor = [System.Windows.Forms.AnchorStyles]::Top
|
||||||
$form.Controls.Add($dataGridView)
|
$form.Controls.Add($dataGridView)
|
||||||
|
|
||||||
# Definieren der Spalten für die DataGridView
|
# Die Breite der DropDown-Liste manuell anpassen, da DropDownWidth für DataGridViewComboBoxColumn nicht unterstützt wird
|
||||||
$dataGridView.ColumnCount = 3
|
$maxLength = 0
|
||||||
$dataGridView.Columns[0].Name = "Typ" # Typ in der ersten Spalte
|
foreach ($item in $masterGroupOUComboBox.Items) {
|
||||||
$dataGridView.Columns[1].Name = "Gruppenname" # Gruppenname in der zweiten Spalte
|
$itemLength = $item.ToString().Length
|
||||||
$dataGridView.Columns[2].Name = "OU" # OU in der dritten Spalte
|
if ($itemLength -gt $maxLength) {
|
||||||
|
$maxLength = $itemLength
|
||||||
# Breite der Spalten festlegen
|
}
|
||||||
$dataGridView.Columns[0].Width = 75 # Typ-Spalte auf 50 Pixel setzen
|
|
||||||
$dataGridView.Columns[1].Width = 100 # Gruppenname-Spalte auf 100 Pixel setzen
|
|
||||||
$dataGridView.Columns[2].Width = 345 # OU-Spalte auf 400 Pixel setzen
|
|
||||||
|
|
||||||
# Dropdown für den Typ (GG oder DL)
|
|
||||||
$typColumn = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
|
|
||||||
$typColumn.HeaderText = "Typ"
|
|
||||||
$typColumn.Items.Add("GG")
|
|
||||||
$typColumn.Items.Add("DL")
|
|
||||||
$typColumn.Items.Add("DL_OS")
|
|
||||||
$dataGridView.Columns.RemoveAt(0)
|
|
||||||
$dataGridView.Columns.Insert(0, $typColumn)
|
|
||||||
# Breite der Typ-Spalte setzen
|
|
||||||
$typColumn.Width = 75 # Du kannst hier den Wert nach Bedarf anpassen
|
|
||||||
|
|
||||||
# Dropdown für die OU-Auswahl in der dritten Spalte
|
|
||||||
$ouColumn = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
|
|
||||||
$ouColumn.HeaderText = "OU"
|
|
||||||
$groupOUList = Get-ADOrganizationalUnit -Filter * | Select-Object -ExpandProperty DistinguishedName
|
|
||||||
foreach ($ou in $groupOUList) {
|
|
||||||
$ouColumn.Items.Add($ou)
|
|
||||||
}
|
}
|
||||||
$dataGridView.Columns.RemoveAt(2)
|
|
||||||
$dataGridView.Columns.Insert(2, $ouColumn)
|
|
||||||
|
|
||||||
# Breite der OU-Spalte setzen
|
# Definieren der Spalten für die DataGridView
|
||||||
$ouColumn.Width = 345 # Du kannst hier den Wert nach Bedarf anpassen
|
$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 = "Standardpasswort" # Standardpasswort
|
||||||
|
$dataGridView.Columns[5].Name = "OU" # OU (wird als ComboBox hinzugefügt)
|
||||||
|
$dataGridView.Columns[5].Width = $ouComboBoxColumn
|
||||||
|
|
||||||
# RichTextBox für Ausgaben
|
# 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(5) # Entfernt die ursprüngliche OU-Spalte
|
||||||
|
$dataGridView.Columns.Insert(5, $ouComboBoxColumn) # Fügt die ComboBox-Spalte an der richtigen Stelle ein
|
||||||
|
$ouComboBoxColumn.Width = $maxLength * 6
|
||||||
|
|
||||||
|
# RichTextBox für Ausgaben (anstelle von TextBox)
|
||||||
$outputTextBox = New-Object System.Windows.Forms.RichTextBox
|
$outputTextBox = New-Object System.Windows.Forms.RichTextBox
|
||||||
$outputTextBox.Multiline = $true
|
$outputTextBox.Multiline = $true
|
||||||
$outputTextBox.Location = New-Object System.Drawing.Point(10, 320) # Position unterhalb des DataGridViews
|
$outputTextBox.Location = New-Object System.Drawing.Point(10, 350) # Position unterhalb des DataGridViews
|
||||||
$outputTextBox.Size = New-Object System.Drawing.Size(564, 150) # Größe der RichTextBox
|
$outputTextBox.Size = New-Object System.Drawing.Size(954, 150) # Größe der RichTextBox festgelegt
|
||||||
$outputTextBox.ScrollBars = 'Vertical'
|
$outputTextBox.ScrollBars = 'Vertical'
|
||||||
$form.Controls.Add($outputTextBox)
|
$form.Controls.Add($outputTextBox)
|
||||||
|
|
||||||
# OK-Button für Gruppen erstellen
|
# OK-Button erstellen
|
||||||
$okButton = New-Object System.Windows.Forms.Button
|
$okButton = New-Object System.Windows.Forms.Button
|
||||||
$okButton.Text = "Gruppen erstellen"
|
$okButton.Text = "Benutzer erstellen"
|
||||||
$okButton.Size = New-Object System.Drawing.Size(280, 30) # Größe des Buttons
|
$okButton.Size = New-Object System.Drawing.Size(380, 30) # Größe des Buttons festgelegt
|
||||||
$okButton.Location = New-Object System.Drawing.Point(10, 475) # Position des Buttons
|
$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
|
|
||||||
$groupType = $row.Cells[0].Value
|
|
||||||
$groupName = $row.Cells[1].Value
|
|
||||||
$groupOU = $row.Cells[2].Value
|
|
||||||
|
|
||||||
if (-not $groupType -or -not $groupName -or -not $groupOU) {
|
|
||||||
$outputTextBox.SelectionColor = 'Red'
|
|
||||||
$outputTextBox.AppendText("Fehler: Alle Felder (Typ, Gruppenname, OU) müssen ausgefüllt sein.`r`n")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
# Präfix vor den Gruppennamen setzen, basierend auf dem Typ
|
|
||||||
if ($groupType -eq "GG") {
|
|
||||||
$groupName = "GG_" + $groupName # Für globale Gruppen "GG_" voranstellen
|
|
||||||
} elseif ($groupType -eq "DL_OS") {
|
|
||||||
$groupName = "DL_" + $groupName # Für globale Gruppen "DL_OS" voranstellen
|
|
||||||
} elseif ($groupType -eq "DL") {
|
|
||||||
$groupName = "DL_" + $groupName # Für DomainLocal Gruppen "DL_" voranstellen
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if ($groupType -eq "GG") {
|
|
||||||
# Globale Gruppe erstellen (keine Suffixe, aber Präfix wird hinzugefügt)
|
|
||||||
$group = Get-ADGroup -Filter { Name -eq $groupName }
|
|
||||||
if (-not $group) {
|
|
||||||
New-ADGroup -Name $groupName `
|
|
||||||
-GroupScope Global `
|
|
||||||
-Path $groupOU `
|
|
||||||
-Description "Globale Gruppe für $groupName"
|
|
||||||
$outputTextBox.SelectionColor = 'Green'
|
|
||||||
$outputTextBox.AppendText("Globale Gruppe '$groupName' wurde erfolgreich erstellt.`r`n")
|
|
||||||
} else {
|
|
||||||
$outputTextBox.SelectionColor = 'Green'
|
|
||||||
$outputTextBox.AppendText("Globale Gruppe '$groupName' existiert bereits.`r`n")
|
|
||||||
}
|
|
||||||
} elseif ($groupType -eq "DL_OS") {
|
|
||||||
# DomainLocal Gruppen ohne Suffix erstellen
|
|
||||||
$domainLocalGroupName = "$groupName"
|
|
||||||
if (-not $group) {
|
|
||||||
New-ADGroup -Name $domainLocalGroupName `
|
|
||||||
-GroupScope DomainLocal `
|
|
||||||
-Path $groupOU `
|
|
||||||
-Description "DomainLocal Gruppe für $domainLocalGroupName"
|
|
||||||
$outputTextBox.SelectionColor = 'Green'
|
|
||||||
$outputTextBox.AppendText("DomainLocal Gruppe '$domainLocalGroupName' wurde erfolgreich erstellt.`r`n")
|
|
||||||
} else {
|
|
||||||
$outputTextBox.SelectionColor = 'Green'
|
|
||||||
$outputTextBox.AppendText("DomainLocal Gruppe '$domainLocalGroupName' existiert bereits.`r`n")
|
|
||||||
}
|
|
||||||
} elseif ($groupType -eq "DL") {
|
|
||||||
# DomainLocal Gruppen mit Suffixen erstellen
|
|
||||||
$suffixes = "_FA", "_RW", "_RX", "_RO"
|
|
||||||
foreach ($suffix in $suffixes) {
|
|
||||||
$domainLocalGroupName = "$groupName$suffix"
|
|
||||||
$group = Get-ADGroup -Filter { Name -eq $domainLocalGroupName }
|
|
||||||
if (-not $group) {
|
|
||||||
New-ADGroup -Name $domainLocalGroupName `
|
|
||||||
-GroupScope DomainLocal `
|
|
||||||
-Path $groupOU `
|
|
||||||
-Description "DomainLocal Gruppe für $domainLocalGroupName"
|
|
||||||
$outputTextBox.SelectionColor = 'Green'
|
|
||||||
$outputTextBox.AppendText("DomainLocal Gruppe '$domainLocalGroupName' wurde erfolgreich erstellt.`r`n")
|
|
||||||
} else {
|
|
||||||
$outputTextBox.SelectionColor = 'Green'
|
|
||||||
$outputTextBox.AppendText("DomainLocal Gruppe '$domainLocalGroupName' existiert bereits.`r`n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
# Fehler bei der Gruppen-Erstellung
|
|
||||||
$outputTextBox.SelectionColor = 'Red'
|
|
||||||
$outputTextBox.AppendText("Fehler bei der Erstellung der Gruppe '$groupName': $_.Exception.Message`r`n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
# Beenden-Button erstellen
|
# Beenden-Button erstellen
|
||||||
$exitButton = New-Object System.Windows.Forms.Button
|
$exitButton = New-Object System.Windows.Forms.Button
|
||||||
$exitButton.Text = "Beenden"
|
$exitButton.Text = "Beenden"
|
||||||
$exitButton.Size = New-Object System.Drawing.Size(280, 30)
|
$exitButton.Size = New-Object System.Drawing.Size(380, 30) # Größe des Buttons festgelegt
|
||||||
$exitButton.Location = New-Object System.Drawing.Point(295, 475) # Position des Beenden-Buttons
|
$exitButton.Location = New-Object System.Drawing.Point(585, 510) # Position des Beenden-Buttons
|
||||||
$exitButton.Add_Click({
|
|
||||||
$form.Close() # Formular schließen
|
|
||||||
})
|
|
||||||
|
|
||||||
# Buttons zum Formular hinzufügen
|
# Buttons zum Formular hinzufügen
|
||||||
$form.Controls.Add($okButton)
|
$form.Controls.Add($okButton)
|
||||||
$form.Controls.Add($exitButton)
|
$form.Controls.Add($exitButton)
|
||||||
|
|
||||||
|
# Event-Handler für den OK-Button hinzufügen
|
||||||
|
$okButton.Add_Click({
|
||||||
|
$masterPassword = $masterPasswordTextBox.Text # Masterkennwort holen
|
||||||
|
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
|
||||||
|
$password = $row.Cells[4].Value
|
||||||
|
$ou = $row.Cells[5].Value # Hier wird die ausgewählte OU aus der ComboBox abgerufen
|
||||||
|
|
||||||
|
# Wenn keine OU in der Zeile gewählt wurde, nehme die Master-OU
|
||||||
|
if (-not $ou) {
|
||||||
|
$ou = $masterOUComboBox.SelectedItem
|
||||||
|
}
|
||||||
|
|
||||||
|
# Wenn kein Passwort angegeben wurde, nutze das Masterkennwort
|
||||||
|
if (-not $password) {
|
||||||
|
$password = $masterPassword
|
||||||
|
}
|
||||||
|
|
||||||
|
# Ü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()
|
||||||
|
|
||||||
|
# E-Mail-Adresse generieren
|
||||||
|
$email = Get-EmailAddress -username $username
|
||||||
|
|
||||||
|
# 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
|
||||||
|
try {
|
||||||
|
New-ADUser -Name "$firstName $lastName" `
|
||||||
|
-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")
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$outputTextBox.SelectionColor = 'Red'
|
||||||
|
$outputTextBox.AppendText("Fehler bei der Erstellung des Benutzers '$firstName $lastName': $_.Exception.Message`r`n")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
# Erfolgsnachricht für vorhandenen Benutzer
|
||||||
|
$outputTextBox.SelectionColor = 'Green'
|
||||||
|
$outputTextBox.AppendText("Benutzer $username existiert bereits.`r`n")
|
||||||
|
}
|
||||||
|
|
||||||
|
# Gruppenzuordnung durchführen, auch wenn der Benutzer schon existiert
|
||||||
|
if ($globalGroup) {
|
||||||
|
# Gruppen-OU immer auf die Master-OU setzen
|
||||||
|
$groupOU = $masterGroupOU
|
||||||
|
try {
|
||||||
|
# Überprüfen, ob die Gruppe existiert
|
||||||
|
$group = Get-ADGroup -Filter { Name -eq $globalGroup }
|
||||||
|
try {
|
||||||
|
# Gruppe erstellen, falls sie nicht existiert
|
||||||
|
$groupName = "GG_" + $globalGroup
|
||||||
|
New-ADGroup -Name $groupName `
|
||||||
|
-GroupScope Global `
|
||||||
|
-Path $groupOU `
|
||||||
|
-Description "Globale Gruppe für $groupName" `
|
||||||
|
-Enabled $true
|
||||||
|
# Erfolgsnachricht
|
||||||
|
$outputTextBox.SelectionColor = 'Green'
|
||||||
|
$outputTextBox.AppendText("Globale Gruppe '$groupName' wurde erfolgreich erstellt.`r`n")
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$outputTextBox.SelectionColor = 'Red'
|
||||||
|
$outputTextBox.AppendText("Fehler bei der Erstellung der Gruppe '$groupName': $_.Exception.Message`r`n")
|
||||||
|
$outputTextBox.AppendText("Fehlerdetails: $_`r`n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
# Fehler bei der Gruppenzuordnung
|
||||||
|
$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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
# Event-Handler für den Beenden-Button hinzufügen
|
||||||
|
$exitButton.Add_Click({
|
||||||
|
$form.Close() # Formular schließen
|
||||||
|
})
|
||||||
|
|
||||||
|
# Funktion zum Erstellen der E-Mail-Adresse
|
||||||
|
function Get-EmailAddress {
|
||||||
|
param (
|
||||||
|
[string]$username
|
||||||
|
)
|
||||||
|
|
||||||
|
return "$username@$domain"
|
||||||
|
}
|
||||||
|
|
||||||
# Formular anzeigen
|
# Formular anzeigen
|
||||||
$form.ShowDialog()
|
$form.ShowDialog()
|
||||||
|
|||||||
Reference in New Issue
Block a user