diff --git a/scripts/ad_users_sa_window.ps1 b/scripts/ad_users_sa_window.ps1 index 0515040..7a17381 100644 --- a/scripts/ad_users_sa_window.ps1 +++ b/scripts/ad_users_sa_window.ps1 @@ -37,39 +37,42 @@ $dataGridView.AlternatingRowsDefaultCellStyle.BackColor = [System.Drawing.Color] $dataGridView.add_CellEndEdit({ param ($sender, $e) $row = $dataGridView.Rows[$e.RowIndex] - - # Felder 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 - } - # Name und SAMAccountName generieren + # 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() + + # 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 von der darüber liegenden Master-Zeile übernehmen - $aboveMasterRow = $null - for ($i = ($e.RowIndex - 1); $i -ge 0; $i--) { - if ($dataGridView.Rows[$i].Cells["Type"].Value -eq 'Master') { - $aboveMasterRow = $dataGridView.Rows[$i] - break + # 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 + } } } - if ($aboveMasterRow -ne $null) { - $row.Cells["OU"].Value = $aboveMasterRow.Cells["OU"].Value - $row.Cells["DC"].Value = $aboveMasterRow.Cells["DC"].Value + + # 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 } }) @@ -117,11 +120,9 @@ foreach ($row in $data) { 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 + + $row.OU = if ($row.OU) { $row.OU } else { $aboveMasterRow.OU } + $row.DC = if ($row.DC) { $row.DC } else { $aboveMasterRow.DC } } } @@ -138,7 +139,9 @@ foreach ($row in $data) { $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() + $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"