165 lines
6.4 KiB
PowerShell
165 lines
6.4 KiB
PowerShell
# 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 }
|