scripts/ad_gg+lg.ps1 hinzugefügt

This commit is contained in:
2024-11-05 12:22:06 +01:00
parent 29d381294c
commit 966fbfcfff

113
scripts/ad_gg+lg.ps1 Normal file
View File

@@ -0,0 +1,113 @@
# 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 = "Gruppen-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 = 4
# Spaltenüberschriften festlegen
$dataGridView.Columns[0].Name = "Type"
$dataGridView.Columns[1].Name = "GroupName"
$dataGridView.Columns[2].Name = "OU"
$dataGridView.Columns[3].Name = "DC"
# Beispielzeilen hinzufügen
$dataGridView.Rows.Add("Master", "DefaultGroup", "DefaultOU", "DC=yourdomain,DC=com")
$dataGridView.Rows.Add("GG", "", "", "")
$dataGridView.Rows.Add("LG", "", "", "")
# DataGridView anpassen
$dataGridView.AlternatingRowsDefaultCellStyle.BackColor = [System.Drawing.Color]::LightGray
# Eventhandler hinzufügen
$dataGridView.add_CellEndEdit({
param ($sender, $e)
$row = $dataGridView.Rows[$e.RowIndex]
# 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
}
}
# Typ basierend auf dem Gruppentyp festlegen
if ($row.Cells["Type"].Value -eq 'GG') {
$row.Cells["GroupType"].Value = 'Global'
} elseif ($row.Cells["Type"].Value -eq 'LG') {
$row.Cells["GroupType"].Value = 'Local'
}
}
})
# 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
GroupName = $row.Cells[1].Value
OU = $row.Cells[2].Value
DC = $row.Cells[3].Value
GroupType = if ($row.Cells[0].Value -eq 'GG') { 'Global' } else { 'Local' }
}
}
}
# Liste für erstellte Gruppen
$createdGroups = @()
# Durchlaufen jeder Zeile der Daten und Gruppen erstellen
foreach ($row in $data) {
if ($row.Type -ne 'Master') {
$path = "OU=$($row.OU),DC=$($row.DC)"
if ($row.Type -eq 'GG') {
# Globale Gruppe erstellen
$newGroup = New-ADGroup -Name $row.GroupName -GroupScope 'Global' -Path $path
$createdGroups += $newGroup
} elseif ($row.Type -eq 'LG') {
# Lokale Gruppen mit Suffixen erstellen
$groupNames = @($row.GroupName + '_FA', $row.GroupName + '_RW', $row.GroupName + '_RX', $row.GroupName + '_RO')
foreach ($groupName in $groupNames) {
$newGroup = New-ADGroup -Name $groupName -GroupScope 'Local' -Path $path
$createdGroups += $newGroup
}
}
}
}
# Erstellte Gruppen anzeigen
Write-Host "`nErstellte Gruppen:" -ForegroundColor Green
$createdGroups | ForEach-Object { Write-Host $_.Name -ForegroundColor Green }