scripts/ad_users_sa_window.ps1 aktualisiert

This commit is contained in:
2024-10-25 09:56:08 +02:00
parent 0aae3a61ad
commit ef48062f27

View File

@@ -38,19 +38,14 @@ $dataGridView.add_CellEndEdit({
param ($sender, $e) param ($sender, $e)
$row = $dataGridView.Rows[$e.RowIndex] $row = $dataGridView.Rows[$e.RowIndex]
# Felder schreibgeschützt machen, wenn Typ SA ist # Name generieren
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
}
# Name und SAMAccountName 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 "") { 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 $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()
# 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 $row.Cells["SAMAccountName"].Value = $samAccountName
# UPN generieren # UPN generieren
@@ -59,17 +54,25 @@ $dataGridView.add_CellEndEdit({
$row.Cells["UPN"].Value = "$samAccountName@$upnDomain" $row.Cells["UPN"].Value = "$samAccountName@$upnDomain"
} }
# OU und DC von der darüber liegenden Master-Zeile übernehmen # OU und DC vom darüberliegenden Master übernehmen
$aboveMasterRow = $null if ($row.Cells["Type"].Value -ne 'Master') {
for ($i = ($e.RowIndex - 1); $i -ge 0; $i--) { for ($i = ($e.RowIndex - 1); $i -ge 0; $i--) {
if ($dataGridView.Rows[$i].Cells["Type"].Value -eq 'Master') { if ($dataGridView.Rows[$i].Cells["Type"].Value -eq 'Master') {
$aboveMasterRow = $dataGridView.Rows[$i] $masterRow = $dataGridView.Rows[$i]
break $row.Cells["OU"].Value = $masterRow.Cells["OU"].Value
$row.Cells["DC"].Value = $masterRow.Cells["DC"].Value
break
}
} }
} }
if ($aboveMasterRow -ne $null) {
$row.Cells["OU"].Value = $aboveMasterRow.Cells["OU"].Value # FirstName und LastName-Feld schreibgeschützt machen, wenn Typ SA ist
$row.Cells["DC"].Value = $aboveMasterRow.Cells["DC"].Value 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
} }
}) })
@@ -118,10 +121,8 @@ foreach ($row in $data) {
} }
} }
$ou = if ($row.OU) { $row.OU } else { $aboveMasterRow.OU } $row.OU = if ($row.OU) { $row.OU } else { $aboveMasterRow.OU }
$dc = if ($row.DC) { $row.DC } else { $aboveMasterRow.DC } $row.DC = if ($row.DC) { $row.DC } else { $aboveMasterRow.DC }
$row.OU = $ou
$row.DC = $dc
} }
} }
@@ -138,7 +139,9 @@ foreach ($row in $data) {
$name = if ($row.Name) { $row.Name } else { $row.FirstName + " " + $row.LastName } $name = if ($row.Name) { $row.Name } else { $row.FirstName + " " + $row.LastName }
# SAMAccountName und UPN generieren # SAMAccountName und UPN generieren
$samAccountName = $row.FirstName.Substring(0, 1).ToLower() + $row.LastName.ToLower() $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 "," $dcParts = $row.DC -replace "DC=", "" -split ","
$upnDomain = ($dcParts -join ".").ToLower() $upnDomain = ($dcParts -join ".").ToLower()
$upn = "$samAccountName@$upnDomain" $upn = "$samAccountName@$upnDomain"