scripts/ad_create_users+serviceaccounts.ps1 gelöscht

This commit is contained in:
2024-11-06 14:51:29 +01:00
parent aa483e1e7a
commit a8a5324783

View File

@@ -1,164 +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 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("Master", "", "", "", "", "", "DefaultOU", "DC=yourdomain,DC=com")
$dataGridView.Rows.Add("User", "", "", "", "", "", "", "")
$dataGridView.Rows.Add("SA", "", "", "", "", "", "", "")
# DataGridView anpassen
$dataGridView.AlternatingRowsDefaultCellStyle.BackColor = [System.Drawing.Color]::LightGray
# Eventhandler hinzufügen, um Name, SAMAccountName und UPN in Echtzeit zu generieren und OU/DC zu übernehmen
$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 generieren
$firstName = $row.Cells["FirstName"].Value.ToLower() -replace "ä", "ae" -replace "ö", "oe" -replace "ü", "ue"
$lastName = $row.Cells["LastName"].Value.ToLower() -replace "ä", "ae" -replace "ö", "oe" -replace "ü", "ue"
$samAccountName = $firstName.Substring(0, 1) + $lastName
$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"
}
# OU und DC vom darüberliegenden Master übernehmen
if ($row.Cells["Type"].Value -ne 'Master') {
for ($i = ($e.RowIndex - 1); $i -ge 0; $i--) {
if ($dataGridView.Rows[$i].Cells["Type"].Value -eq 'Master') {
$masterRow = $dataGridView.Rows[$i]
$row.Cells["OU"].Value = $masterRow.Cells["OU"].Value
$row.Cells["DC"].Value = $masterRow.Cells["DC"].Value
break
}
}
}
# FirstName und LastName-Feld schreibgeschützt machen, wenn Typ SA ist
if ($row.Cells["Type"].Value -eq 'SA') {
$row.Cells["FirstName"].ReadOnly = $true
$row.Cells["LastName"].ReadOnly = $true
} else {
$row.Cells["FirstName"].ReadOnly = $false
$row.Cells["LastName"].ReadOnly = $false
}
})
# 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
}
}
$row.OU = if ($row.OU) { $row.OU } else { $aboveMasterRow.OU }
$row.DC = if ($row.DC) { $row.DC } else { $aboveMasterRow.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
$firstName = $row.FirstName.ToLower() -replace "ä", "ae" -replace "ö", "oe" -replace "ü", "ue"
$lastName = $row.LastName.ToLower() -replace "ä", "ae" -replace "ö", "oe" -replace "ü", "ue"
$samAccountName = $firstName.Substring(0, 1) + $lastName
$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 }