scripts/ad_users_sa_window.ps1 aktualisiert
This commit is contained in:
@@ -1,82 +0,0 @@
|
|||||||
# Erforderliche Assemblies laden
|
|
||||||
Add-Type -AssemblyName System.Windows.Forms
|
|
||||||
Add-Type -AssemblyName System.Drawing
|
|
||||||
|
|
||||||
# Formular erstellen
|
|
||||||
$form = New-Object System.Windows.Forms.Form
|
|
||||||
$form.Text = "Benutzer- und Gruppenerstellung"
|
|
||||||
$form.Size = New-Object System.Drawing.Size(500, 600)
|
|
||||||
$form.StartPosition = "CenterScreen"
|
|
||||||
|
|
||||||
# Labels und Textboxen erstellen
|
|
||||||
$labels = @("Type", "Name", "FirstName", "LastName", "SAMAccountName", "UPN", "OU", "DC", "Password", "Group", "GroupOU")
|
|
||||||
$positions = 20
|
|
||||||
$textBoxes = @{}
|
|
||||||
|
|
||||||
foreach ($label in $labels) {
|
|
||||||
$labelControl = New-Object System.Windows.Forms.Label
|
|
||||||
$labelControl.Text = $label
|
|
||||||
$labelControl.Size = New-Object System.Drawing.Size(120, 20)
|
|
||||||
$labelControl.Location = New-Object System.Drawing.Point(20, $positions)
|
|
||||||
$form.Controls.Add($labelControl)
|
|
||||||
|
|
||||||
$textBox = New-Object System.Windows.Forms.TextBox
|
|
||||||
$textBox.Size = New-Object System.Drawing.Size(300, 20)
|
|
||||||
$textBox.Location = New-Object System.Drawing.Point(150, $positions)
|
|
||||||
$form.Controls.Add($textBox)
|
|
||||||
$textBoxes[$label] = $textBox
|
|
||||||
|
|
||||||
$positions += 30
|
|
||||||
}
|
|
||||||
|
|
||||||
# OK-Button
|
|
||||||
$okButton = New-Object System.Windows.Forms.Button
|
|
||||||
$okButton.Text = "OK"
|
|
||||||
$okButton.Location = New-Object System.Drawing.Point(200, $positions)
|
|
||||||
$okButton.Add_Click({
|
|
||||||
$form.Close()
|
|
||||||
})
|
|
||||||
$form.Controls.Add($okButton)
|
|
||||||
|
|
||||||
# Formular anzeigen
|
|
||||||
$form.ShowDialog()
|
|
||||||
|
|
||||||
# Eingabedaten extrahieren
|
|
||||||
$data = @{
|
|
||||||
Type = $textBoxes["Type"].Text
|
|
||||||
Name = $textBoxes["Name"].Text
|
|
||||||
FirstName = $textBoxes["FirstName"].Text
|
|
||||||
LastName = $textBoxes["LastName"].Text
|
|
||||||
SAMAccountName = $textBoxes["SAMAccountName"].Text
|
|
||||||
UPN = $textBoxes["UPN"].Text
|
|
||||||
OU = $textBoxes["OU"].Text
|
|
||||||
DC = $textBoxes["DC"].Text
|
|
||||||
Password = $textBoxes["Password"].Text
|
|
||||||
Group = $textBoxes["Group"].Text
|
|
||||||
GroupOU = $textBoxes["GroupOU"].Text
|
|
||||||
}
|
|
||||||
|
|
||||||
# Überprüfen, ob die OU und DC Werte haben
|
|
||||||
if (-not $data.OU) { $data.OU = "DefaultOU" }
|
|
||||||
if (-not $data.DC) { $data.DC = "DefaultDC" }
|
|
||||||
|
|
||||||
# Benutzer oder Gruppe erstellen basierend auf dem Typ
|
|
||||||
if ($data.Type -eq 'User' -or $data.Type -eq 'SA') {
|
|
||||||
$name = if ($data.Name) { $data.Name } else { $data.FirstName + " " + $data.LastName }
|
|
||||||
if (-not (Get-ADUser -Filter "SamAccountName -eq '$($data.SAMAccountName)'")) {
|
|
||||||
New-ADUser -Name $name -GivenName $data.FirstName -Surname $data.LastName -SamAccountName $data.SAMAccountName -UserPrincipalName $data.UPN -Path "OU=$($data.OU),DC=$($data.DC)" -AccountPassword (ConvertTo-SecureString $data.Password -AsPlainText -Force) -Enabled $true
|
|
||||||
$groups = $data.Group -split ','
|
|
||||||
foreach ($group in $groups) {
|
|
||||||
if (Get-ADGroup -Filter "Name -eq '$group'") {
|
|
||||||
Add-ADGroupMember -Identity $group -Members $data.SAMAccountName
|
|
||||||
} else {
|
|
||||||
New-ADGroup -Name $group -GroupScope Global -Path "OU=$($data.GroupOU),DC=$($data.DC)"
|
|
||||||
Add-ADGroupMember -Identity $group -Members $data.SAMAccountName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} elseif ($data.Type -eq 'Group') {
|
|
||||||
if (-not (Get-ADGroup -Filter "Name -eq '$($data.Group)'")) {
|
|
||||||
New-ADGroup -Name $data.Group -GroupScope Global -Path "OU=$($data.GroupOU),DC=$($data.DC)"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
164
scripts/ad_users_sa_window.ps1
Normal file
164
scripts/ad_users_sa_window.ps1
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
# Erforderliche Assemblies laden
|
||||||
|
Add-Type -AssemblyName System.Windows.Forms
|
||||||
|
Add-Type -AssemblyName System.Drawing
|
||||||
|
|
||||||
|
# Formular erstellen
|
||||||
|
$form = New-Object System.Windows.Forms.Form
|
||||||
|
$form.Text = "Benutzer- und SA-Erstellung"
|
||||||
|
$form.Size = New-Object System.Drawing.Size(1200, 650)
|
||||||
|
$form.StartPosition = "CenterScreen"
|
||||||
|
$form.BackColor = [System.Drawing.Color]::White
|
||||||
|
|
||||||
|
# DataGridView erstellen
|
||||||
|
$dataGridView = New-Object System.Windows.Forms.DataGridView
|
||||||
|
$dataGridView.Size = New-Object System.Drawing.Size(1150, 450)
|
||||||
|
$dataGridView.Location = New-Object System.Drawing.Point(15, 15)
|
||||||
|
$dataGridView.ColumnCount = 8
|
||||||
|
|
||||||
|
# Spaltenüberschriften festlegen
|
||||||
|
$dataGridView.Columns[0].Name = "Type"
|
||||||
|
$dataGridView.Columns[1].Name = "FirstName"
|
||||||
|
$dataGridView.Columns[2].Name = "LastName"
|
||||||
|
$dataGridView.Columns[3].Name = "Name"
|
||||||
|
$dataGridView.Columns[4].Name = "SAMAccountName"
|
||||||
|
$dataGridView.Columns[5].Name = "UPN"
|
||||||
|
$dataGridView.Columns[6].Name = "OU"
|
||||||
|
$dataGridView.Columns[7].Name = "DC"
|
||||||
|
|
||||||
|
# Beispielzeilen hinzufügen
|
||||||
|
$dataGridView.Rows.Add("User", "", "", "", "", "", "", "")
|
||||||
|
$dataGridView.Rows.Add("SA", "", "", "", "", "", "", "")
|
||||||
|
|
||||||
|
# DataGridView anpassen
|
||||||
|
$dataGridView.AlternatingRowsDefaultCellStyle.BackColor = [System.Drawing.Color]::LightGray
|
||||||
|
|
||||||
|
# Eventhandler hinzufügen, um Name und SAMAccountName in Echtzeit zu generieren
|
||||||
|
$dataGridView.add_CellEndEdit({
|
||||||
|
param ($sender, $e)
|
||||||
|
$row = $dataGridView.Rows[$e.RowIndex]
|
||||||
|
|
||||||
|
# Name generieren
|
||||||
|
if ($row.Cells["FirstName"].Value -ne $null -and $row.Cells["LastName"].Value -ne $null -and $row.Cells["FirstName"].Value -ne "" -and $row.Cells["LastName"].Value -ne "") {
|
||||||
|
$row.Cells["Name"].Value = $row.Cells["FirstName"].Value + " " + $row.Cells["LastName"].Value
|
||||||
|
$samAccountName = $row.Cells["FirstName"].Value.Substring(0, 1).ToLower() + $row.Cells["LastName"].Value.ToLower()
|
||||||
|
$row.Cells["SAMAccountName"].Value = $samAccountName
|
||||||
|
|
||||||
|
# UPN generieren
|
||||||
|
$dcParts = $row.Cells["DC"].Value -replace "DC=", "" -split ","
|
||||||
|
$upnDomain = ($dcParts -join ".").ToLower()
|
||||||
|
$row.Cells["UPN"].Value = "$samAccountName@$upnDomain"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Alle Felder außer Type ein- oder ausblenden
|
||||||
|
if ($row.Cells["Type"].Value -ne 'User' -and $row.Cells["Type"].Value -ne 'SA' -and $row.Cells["Type"].Value -ne 'Master') {
|
||||||
|
for ($i = 1; $i -lt $row.Cells.Count; $i++) {
|
||||||
|
$row.Cells[$i].Style.BackColor = $form.BackColor
|
||||||
|
$row.Cells[$i].Style.ForeColor = $form.BackColor
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for ($i = 1; $i -lt $row.Cells.Count; $i++) {
|
||||||
|
$row.Cells[$i].Style.BackColor = [System.Drawing.Color]::White
|
||||||
|
$row.Cells[$i].Style.ForeColor = [System.Drawing.Color]::Black
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# FirstName und LastName-Feld durchsichtig machen oder ausblenden, wenn Typ SA oder Master ist
|
||||||
|
if ($row.Cells["Type"].Value -eq 'SA' -or $row.Cells["Type"].Value -eq 'Master') {
|
||||||
|
$row.Cells["FirstName"].Style.BackColor = $form.BackColor
|
||||||
|
$row.Cells["FirstName"].Style.ForeColor = $form.BackColor
|
||||||
|
$row.Cells["LastName"].Style.BackColor = $form.BackColor
|
||||||
|
$row.Cells["LastName"].Style.ForeColor = $form.BackColor
|
||||||
|
} else {
|
||||||
|
$row.Cells["FirstName"].Style.BackColor = [System.Drawing.Color]::White
|
||||||
|
$row.Cells["FirstName"].Style.ForeColor = [System.Drawing.Color]::Black
|
||||||
|
$row.Cells["LastName"].Style.BackColor = [System.Drawing.Color]::White
|
||||||
|
$row.Cells["LastName"].Style.ForeColor = [System.Drawing.Color]::Black
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
# OK-Button
|
||||||
|
$okButton = New-Object System.Windows.Forms.Button
|
||||||
|
$okButton.Text = "OK"
|
||||||
|
$okButton.Location = New-Object System.Drawing.Point(550, 500)
|
||||||
|
$okButton.BackColor = [System.Drawing.Color]::White
|
||||||
|
$okButton.FlatStyle = "Flat"
|
||||||
|
$okButton.Add_Click({
|
||||||
|
$form.Close()
|
||||||
|
})
|
||||||
|
$form.Controls.Add($okButton)
|
||||||
|
|
||||||
|
# DataGridView zum Formular hinzufügen
|
||||||
|
$form.Controls.Add($dataGridView)
|
||||||
|
|
||||||
|
# Formular anzeigen
|
||||||
|
$form.ShowDialog()
|
||||||
|
|
||||||
|
# Eingabedaten extrahieren
|
||||||
|
$data = @()
|
||||||
|
foreach ($row in $dataGridView.Rows) {
|
||||||
|
if (-not $row.IsNewRow) {
|
||||||
|
$data += [PSCustomObject]@{
|
||||||
|
Type = $row.Cells[0].Value
|
||||||
|
FirstName = $row.Cells[1].Value
|
||||||
|
LastName = $row.Cells[2].Value
|
||||||
|
Name = $row.Cells[3].Value
|
||||||
|
SAMAccountName = $row.Cells[4].Value
|
||||||
|
UPN = $row.Cells[5].Value
|
||||||
|
OU = $row.Cells[6].Value
|
||||||
|
DC = $row.Cells[7].Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Für jede Zeile den ersten darüberliegenden Master-Eintrag finden
|
||||||
|
foreach ($row in $data) {
|
||||||
|
if ($row.Type -ne 'Master') {
|
||||||
|
$aboveMasterRow = $null
|
||||||
|
for ($i = ($data.IndexOf($row) - 1); $i -ge 0; $i--) {
|
||||||
|
if ($data[$i].Type -eq 'Master') {
|
||||||
|
$aboveMasterRow = $data[$i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$ou = if ($row.OU) { $row.OU } else { $aboveMasterRow.OU }
|
||||||
|
$dc = if ($row.DC) { $row.DC } else { $aboveMasterRow.DC }
|
||||||
|
$row.OU = $ou
|
||||||
|
$row.DC = $dc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Listen für vorhandene Benutzer
|
||||||
|
$existingUsers = @()
|
||||||
|
|
||||||
|
# Durchlaufen jeder Zeile der Daten
|
||||||
|
foreach ($row in $data) {
|
||||||
|
if ($row.Type -ne 'Master') {
|
||||||
|
$path = "OU=$($row.OU),DC=$($row.DC)"
|
||||||
|
|
||||||
|
if ($row.Type -eq 'User' -or $row.Type -eq 'SA') {
|
||||||
|
# Name aus FirstName und LastName generieren, falls nicht vorhanden
|
||||||
|
$name = if ($row.Name) { $row.Name } else { $row.FirstName + " " + $row.LastName }
|
||||||
|
|
||||||
|
# SAMAccountName und UPN generieren
|
||||||
|
$samAccountName = $row.FirstName.Substring(0, 1).ToLower() + $row.LastName.ToLower()
|
||||||
|
$dcParts = $row.DC -replace "DC=", "" -split ","
|
||||||
|
$upnDomain = ($dcParts -join ".").ToLower()
|
||||||
|
$upn = "$samAccountName@$upnDomain"
|
||||||
|
$row.SAMAccountName = $samAccountName
|
||||||
|
$row.UPN = $upn
|
||||||
|
|
||||||
|
# Überprüfen, ob der Benutzer bereits existiert
|
||||||
|
if (Get-ADUser -Filter "SamAccountName -eq '$($row.SAMAccountName)'") {
|
||||||
|
$existingUsers += $row.SAMAccountName
|
||||||
|
} else {
|
||||||
|
# Erstellung des Benutzers
|
||||||
|
New-ADUser -Name $name -GivenName $row.FirstName -Surname $row.LastName -SamAccountName $row.SAMAccountName -UserPrincipalName $row.UPN -Path $path -AccountPassword (ConvertTo-SecureString "P@ssword1" -AsPlainText -Force) -Enabled $true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Zusammenfassung der vorhandenen Benutzer
|
||||||
|
Write-Host "`nBereits vorhandene Benutzer:" -ForegroundColor Red
|
||||||
|
$existingUsers | ForEach-Object { Write-Host $_ -ForegroundColor Red }
|
||||||
Reference in New Issue
Block a user