diff --git a/scripts/AD_USER_CREATE.ps1 b/scripts/AD_USER_CREATE.ps1 deleted file mode 100644 index 1e1faf3..0000000 --- a/scripts/AD_USER_CREATE.ps1 +++ /dev/null @@ -1,285 +0,0 @@ -$myWindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() -$myPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsIdentity) - -if (-not $myPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) { - $arguments = "& '" + $myinvocation.mycommand.definition + "'" - Start-Process powershell -ArgumentList $arguments -Verb runAs - Exit -} -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(990, 600) # Gesamtgröße der Form anpassen - -# 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.Size = New-Object System.Drawing.Size(954, 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) - -# Die Breite der DropDown-Liste manuell anpassen, da DropDownWidth für DataGridViewComboBoxColumn nicht unterstützt wird -$maxLength = 0 -foreach ($item in $masterGroupOUComboBox.Items) { - $itemLength = $item.ToString().Length - if ($itemLength -gt $maxLength) { - $maxLength = $itemLength - } -} - -# 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 = "Standardpasswort" # Standardpasswort -$dataGridView.Columns[5].Name = "OU" # OU (wird als ComboBox hinzugefügt) -$dataGridView.Columns[5].Width = $ouComboBoxColumn - -# 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.Multiline = $true -$outputTextBox.Location = New-Object System.Drawing.Point(10, 350) # Position unterhalb des DataGridViews -$outputTextBox.Size = New-Object System.Drawing.Size(954, 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(380, 30) # Größe des Buttons festgelegt -$okButton.Location = New-Object System.Drawing.Point(10, 510) # Position des Buttons unter der TextBox - -# Beenden-Button erstellen -$exitButton = New-Object System.Windows.Forms.Button -$exitButton.Text = "Beenden" -$exitButton.Size = New-Object System.Drawing.Size(380, 30) # Größe des Buttons festgelegt -$exitButton.Location = New-Object System.Drawing.Point(585, 510) # Position des Beenden-Buttons - -# Buttons zum Formular hinzufügen -$form.Controls.Add($okButton) -$form.Controls.Add($exitButton) - -# Importiere das Active Directory Modul, falls noch nicht geschehen -Import-Module ActiveDirectory - -# 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 - } - - $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() - - # 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':`r`n") - $outputTextBox.AppendText("Fehlerdetails: $_`r`n") - } - } - else { - # Erfolgsnachricht für vorhandenen Benutzer - $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 - try { - # Überprüfen, ob die Gruppe existiert - $groupOU = $masterGroupOUComboBox.SelectedItem - $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" - - # 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':`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':`r`n") - $outputTextBox.AppendText("Fehlerdetails: $_`r`n") - } - } - } - catch { - # Fehler bei der Benutzererstellung - $outputTextBox.SelectionColor = 'Red' - $outputTextBox.AppendText("Fehler bei der Erstellung des Benutzers '$firstName $lastName':`r`n") - $outputTextBox.AppendText("Fehlerdetails: $_`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 -$form.ShowDialog() \ No newline at end of file