diff --git a/scripts/ad_users_sa_window.ps1 b/scripts/ad_users_sa_window.ps1 index de04359..0515040 100644 --- a/scripts/ad_users_sa_window.ps1 +++ b/scripts/ad_users_sa_window.ps1 @@ -26,19 +26,28 @@ $dataGridView.Columns[6].Name = "OU" $dataGridView.Columns[7].Name = "DC" # Beispielzeilen hinzufügen -$dataGridView.Rows.Add("Master", "", "", "", "", "", "", "") +$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 und SAMAccountName in Echtzeit zu generieren +# 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] + + # 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 generieren + # 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 "") { $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() @@ -50,30 +59,17 @@ $dataGridView.add_CellEndEdit({ $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 + # 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 } } - - # 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 + if ($aboveMasterRow -ne $null) { + $row.Cells["OU"].Value = $aboveMasterRow.Cells["OU"].Value + $row.Cells["DC"].Value = $aboveMasterRow.Cells["DC"].Value } })