scripts/AD_GROUPS_CREATE.ps1 aktualisiert
This commit is contained in:
@@ -1,212 +1,125 @@
|
||||
$runAsAdmin = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
|
||||
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
|
||||
|
||||
if (-not $runAsAdmin.IsInRole($adminRole)) {
|
||||
# Relaunch the script as Administrator
|
||||
$arguments = "$($myinvocation.MyCommand.Definition)"
|
||||
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File $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(800, 600) # Gesamtgröße der Form anpassen
|
||||
$form.Text = "Gruppen Erstellung"
|
||||
$form.Size = New-Object System.Drawing.Size(600, 555) # 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 für Gruppen 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
|
||||
$dataGridView.Size = New-Object System.Drawing.Size(564, 300) # Breite und Höhe des DataGridViews
|
||||
$dataGridView.Location = New-Object System.Drawing.Point(10, 10) # Position des DataGridViews
|
||||
$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 = "Standardpasswort" # Standardpasswort
|
||||
$dataGridView.Columns[5].Name = "OU" # OU (wird als ComboBox hinzugefügt)
|
||||
$dataGridView.Columns[5].Width = 220
|
||||
$dataGridView.ColumnCount = 3
|
||||
$dataGridView.Columns[0].Name = "Typ" # Typ in der ersten Spalte
|
||||
$dataGridView.Columns[1].Name = "Gruppenname" # Gruppenname in der zweiten Spalte
|
||||
$dataGridView.Columns[2].Name = "OU" # OU in der dritten Spalte
|
||||
|
||||
# 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 = 220 # Du kannst hier den Wert nach Bedarf anpassen
|
||||
# Breite der Spalten festlegen
|
||||
$dataGridView.Columns[0].Width = 50 # Typ-Spalte auf 50 Pixel setzen
|
||||
$dataGridView.Columns[1].Width = 100 # Gruppenname-Spalte auf 100 Pixel setzen
|
||||
$dataGridView.Columns[2].Width = 370 # OU-Spalte auf 400 Pixel setzen
|
||||
|
||||
# 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[5].Value = $selectedMasterOU # Zeile aktualisieren, statt der 4. Spalte jetzt die 5. für OU
|
||||
}
|
||||
}
|
||||
# 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")
|
||||
$dataGridView.Columns.RemoveAt(0)
|
||||
$dataGridView.Columns.Insert(0, $typColumn)
|
||||
# Breite der Typ-Spalte setzen
|
||||
$typColumn.Width = 50 # 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)
|
||||
|
||||
# Master-OU ComboBox geändert: Aktualisiere alle OUs im DataGridView
|
||||
$masterOUComboBox.Add_SelectedIndexChanged({
|
||||
Update-OUsInGrid
|
||||
})
|
||||
# Breite der OU-Spalte setzen
|
||||
$ouColumn.Width = 370 # Du kannst hier den Wert nach Bedarf anpassen
|
||||
|
||||
# RichTextBox für Ausgaben (anstelle von TextBox)
|
||||
# RichTextBox für Ausgaben
|
||||
$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.Location = New-Object System.Drawing.Point(10, 320) # Position unterhalb des DataGridViews
|
||||
$outputTextBox.Size = New-Object System.Drawing.Size(564, 150) # Größe der RichTextBox
|
||||
$outputTextBox.ScrollBars = 'Vertical'
|
||||
$form.Controls.Add($outputTextBox)
|
||||
|
||||
# OK-Button erstellen
|
||||
# OK-Button für Gruppen 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
|
||||
$okButton.Text = "Gruppen erstellen"
|
||||
$okButton.Size = New-Object System.Drawing.Size(280, 30) # Größe des Buttons
|
||||
$okButton.Location = New-Object System.Drawing.Point(10, 475) # Position des Buttons
|
||||
$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
|
||||
$password = $row.Cells[4].Value
|
||||
$ou = $row.Cells[5].Value # Hier wird die ausgewählte OU aus der ComboBox abgerufen
|
||||
$groupType = $row.Cells[0].Value
|
||||
$groupName = $row.Cells[1].Value
|
||||
$groupOU = $row.Cells[2].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")
|
||||
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
|
||||
}
|
||||
|
||||
# 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
|
||||
# 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") {
|
||||
$groupName = "DL_" + $groupName # Für DomainLocal Gruppen "DL_" voranstellen
|
||||
}
|
||||
|
||||
# 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 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 ($groupType -eq "GG") {
|
||||
# Globale Gruppe erstellen (keine Suffixe, aber Präfix wird hinzugefügt)
|
||||
$group = Get-ADGroup -Filter { Name -eq $groupName }
|
||||
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
|
||||
New-ADGroup -Name $groupName `
|
||||
-GroupScope Global `
|
||||
-Path $groupOU `
|
||||
-Description "Globale Gruppe für $groupName"
|
||||
$outputTextBox.SelectionColor = 'Green'
|
||||
$outputTextBox.AppendText("Globale Gruppe '$globalGroup' wurde erfolgreich erstellt.`r`n")
|
||||
$outputTextBox.AppendText("Globale Gruppe '$groupName' wurde erfolgreich erstellt.`r`n")
|
||||
} else {
|
||||
$outputTextBox.SelectionColor = 'Green'
|
||||
$outputTextBox.AppendText("Globale Gruppe '$groupName' existiert bereits.`r`n")
|
||||
}
|
||||
# Benutzer der Gruppe zuordnen
|
||||
Add-ADGroupMember -Identity $globalGroup -Members $username
|
||||
} 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("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")
|
||||
$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 Benutzererstellung
|
||||
# Fehler bei der Gruppen-Erstellung
|
||||
$outputTextBox.SelectionColor = 'Red'
|
||||
$outputTextBox.AppendText("Fehler bei der Erstellung des Benutzers '$firstName $lastName': $_.Exception.Message`r`n")
|
||||
$outputTextBox.AppendText("Fehler bei der Erstellung der Gruppe '$groupName': $_.Exception.Message`r`n")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -215,8 +128,8 @@ $okButton.Add_Click({
|
||||
# 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(395, 510) # Position des Beenden-Buttons
|
||||
$exitButton.Size = New-Object System.Drawing.Size(280, 30)
|
||||
$exitButton.Location = New-Object System.Drawing.Point(295, 475) # Position des Beenden-Buttons
|
||||
$exitButton.Add_Click({
|
||||
$form.Close() # Formular schließen
|
||||
})
|
||||
@@ -225,14 +138,5 @@ $exitButton.Add_Click({
|
||||
$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()
|
||||
|
||||
Reference in New Issue
Block a user