scripts/AD_AGDLP.ps1 aktualisiert
This commit is contained in:
@@ -1,13 +1,3 @@
|
||||
$runAsAdmin = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
|
||||
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
|
||||
|
||||
if (-not $runAsAdmin.IsInRole($adminRole)) {
|
||||
# Relaunch the script as Administrator
|
||||
$arguments = "$($myinvocation.MyCommand.Definition)"
|
||||
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File $arguments" -Verb RunAs
|
||||
exit
|
||||
}
|
||||
|
||||
# Import-Module und GUI-Komponenten laden
|
||||
Add-Type -AssemblyName System.Windows.Forms
|
||||
Import-Module ActiveDirectory
|
||||
@@ -23,30 +13,63 @@ function Get-Groups {
|
||||
$groups = Get-ADGroup -Filter *
|
||||
return $groups | ForEach-Object { $_.Name }
|
||||
}
|
||||
|
||||
# Funktion zum Abrufen von Benutzern aus AD
|
||||
function Get-Users {
|
||||
$users = Get-ADUser -Filter *
|
||||
return $users | ForEach-Object { $_.SamAccountName }
|
||||
}
|
||||
|
||||
$domain = Get-DomainName
|
||||
$ggList = Get-ADGroup -Filter { GroupScope -eq 'Global' }
|
||||
$ggList = $ggList | Select-Object -ExpandProperty Name
|
||||
$dlList = Get-Groups
|
||||
$userList = Get-Users
|
||||
|
||||
# GUI zur Zuordnung von GG zu DL erstellen
|
||||
# GUI zur Zuordnung von GG zu DL und Benutzern erstellen
|
||||
$form = New-Object System.Windows.Forms.Form
|
||||
$form.Text = "GG zu DL Zuordnung"
|
||||
$form.Size = New-Object System.Drawing.Size(550, 400) # Großes komfortables Fenster
|
||||
$form.Text = "GG zu DL Zuordnung und Benutzer"
|
||||
$form.Size = New-Object System.Drawing.Size(805, 345) # Fenstergröße anpassen
|
||||
|
||||
# GG-Eingabe und Liste
|
||||
# Benutzer-Eingabe und Liste (ganz oben)
|
||||
$userLabel = New-Object System.Windows.Forms.Label
|
||||
$userLabel.Text = "Benutzer zu Globalgruppe zuordnen:"
|
||||
$userLabel.Location = New-Object System.Drawing.Point(10, 10)
|
||||
$userLabel.Size = New-Object System.Drawing.Size(250, 20)
|
||||
$form.Controls.Add($userLabel)
|
||||
|
||||
$userTextBox = New-Object System.Windows.Forms.TextBox
|
||||
$userTextBox.Location = New-Object System.Drawing.Point(10, 30)
|
||||
$userTextBox.Size = New-Object System.Drawing.Size(250, 20)
|
||||
$form.Controls.Add($userTextBox)
|
||||
|
||||
$userListBox = New-Object System.Windows.Forms.ListBox
|
||||
$userListBox.Location = New-Object System.Drawing.Point(10, 55)
|
||||
$userListBox.Size = New-Object System.Drawing.Size(250, 200) # Anpassung der Größe
|
||||
$userListBox.Items.AddRange($userList)
|
||||
$userListBox.SelectionMode = [System.Windows.Forms.SelectionMode]::MultiExtended
|
||||
$form.Controls.Add($userListBox)
|
||||
|
||||
$userTextBox.Add_TextChanged({
|
||||
$userListBox.Items.Clear()
|
||||
$filteredUsers = $userList | Where-Object { $_ -like "*$($userTextBox.Text)*" }
|
||||
$userListBox.Items.AddRange($filteredUsers)
|
||||
})
|
||||
|
||||
# GG-Eingabe und Liste (mitte)
|
||||
$ggLabel = New-Object System.Windows.Forms.Label
|
||||
$ggLabel.Text = "Globalgruppe (GG):"
|
||||
$ggLabel.Location = New-Object System.Drawing.Point(10, 10)
|
||||
$ggLabel.Location = New-Object System.Drawing.Point(270, 10)
|
||||
$ggLabel.Size = New-Object System.Drawing.Size(250, 20)
|
||||
$form.Controls.Add($ggLabel)
|
||||
|
||||
$ggTextBox = New-Object System.Windows.Forms.TextBox
|
||||
$ggTextBox.Location = New-Object System.Drawing.Point(10, 30)
|
||||
$ggTextBox.Location = New-Object System.Drawing.Point(270, 30)
|
||||
$ggTextBox.Size = New-Object System.Drawing.Size(250, 20)
|
||||
$form.Controls.Add($ggTextBox)
|
||||
|
||||
$ggListBox = New-Object System.Windows.Forms.ListBox
|
||||
$ggListBox.Location = New-Object System.Drawing.Point(10, 55)
|
||||
$ggListBox.Location = New-Object System.Drawing.Point(270, 55)
|
||||
$ggListBox.Size = New-Object System.Drawing.Size(250, 200)
|
||||
$ggListBox.Items.AddRange($ggList)
|
||||
$ggListBox.SelectionMode = [System.Windows.Forms.SelectionMode]::One
|
||||
@@ -58,20 +81,20 @@ $ggTextBox.Add_TextChanged({
|
||||
$ggListBox.Items.AddRange($filteredGGs)
|
||||
})
|
||||
|
||||
# DL-Eingabe und Liste
|
||||
# DL-Eingabe und Liste (ganz unten)
|
||||
$dlLabel = New-Object System.Windows.Forms.Label
|
||||
$dlLabel.Text = "Domänenlokale Gruppen (DL):"
|
||||
$dlLabel.Location = New-Object System.Drawing.Point(270, 10)
|
||||
$dlLabel.Location = New-Object System.Drawing.Point(530, 10)
|
||||
$dlLabel.Size = New-Object System.Drawing.Size(250, 20)
|
||||
$form.Controls.Add($dlLabel)
|
||||
|
||||
$dlTextBox = New-Object System.Windows.Forms.TextBox
|
||||
$dlTextBox.Location = New-Object System.Drawing.Point(270, 30)
|
||||
$dlTextBox.Location = New-Object System.Drawing.Point(530, 30)
|
||||
$dlTextBox.Size = New-Object System.Drawing.Size(250, 20)
|
||||
$form.Controls.Add($dlTextBox)
|
||||
|
||||
$dlListBox = New-Object System.Windows.Forms.ListBox
|
||||
$dlListBox.Location = New-Object System.Drawing.Point(270, 55)
|
||||
$dlListBox.Location = New-Object System.Drawing.Point(530, 55)
|
||||
$dlListBox.Size = New-Object System.Drawing.Size(250, 200)
|
||||
$dlListBox.Items.AddRange($dlList)
|
||||
$dlListBox.SelectionMode = [System.Windows.Forms.SelectionMode]::MultiExtended
|
||||
@@ -83,22 +106,22 @@ $dlTextBox.Add_TextChanged({
|
||||
$dlListBox.Items.AddRange($filteredDLs)
|
||||
})
|
||||
|
||||
|
||||
# OK-Button
|
||||
$okButton = New-Object System.Windows.Forms.Button
|
||||
$okButton.Text = "OK"
|
||||
$okButton.Location = New-Object System.Drawing.Point(80, 300)
|
||||
$okButton.Size = New-Object System.Drawing.Size(100, 30)
|
||||
$okButton.Location = New-Object System.Drawing.Point(10, 265)
|
||||
$okButton.Size = New-Object System.Drawing.Size(380, 30)
|
||||
$okButton.Add_Click({
|
||||
$gg = $ggListBox.SelectedItem
|
||||
$dl = $dlListBox.SelectedItems
|
||||
$users = $userListBox.SelectedItems
|
||||
|
||||
# Zuordnen der Globalgruppe zu Domänenlokalen Gruppen
|
||||
foreach ($dlGroup in $dl) {
|
||||
# Prüfen, ob die Gruppe eine Domänenlokale Gruppe oder eine Globalgruppe ist
|
||||
if (Get-ADGroup -Filter "Name -eq '$dlGroup'") {
|
||||
$groupType = (Get-ADGroup -Identity $dlGroup).GroupScope
|
||||
|
||||
# Falls es eine Domänenlokale Gruppe ist
|
||||
if ($groupType -eq 'DomainLocal') {
|
||||
if (-not (Get-ADGroupMember -Identity $dlGroup -Recursive | Where-Object { $_.SamAccountName -eq $gg })) {
|
||||
Add-ADGroupMember -Identity $dlGroup -Members $gg
|
||||
@@ -107,7 +130,6 @@ $okButton.Add_Click({
|
||||
Write-Output "Globalgruppe $gg ist bereits Mitglied der Domänenlokalen Gruppe $dlGroup."
|
||||
}
|
||||
}
|
||||
# Falls es eine Globalgruppe ist
|
||||
elseif ($groupType -eq 'Global') {
|
||||
if (-not (Get-ADGroupMember -Identity $dlGroup -Recursive | Where-Object { $_.SamAccountName -eq $gg })) {
|
||||
Add-ADGroupMember -Identity $dlGroup -Members $gg
|
||||
@@ -116,20 +138,28 @@ $okButton.Add_Click({
|
||||
Write-Output "Globalgruppe $gg ist bereits Mitglied der Globalgruppe $dlGroup."
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Write-Output "Die Gruppe $dlGroup existiert nicht."
|
||||
}
|
||||
|
||||
# Benutzer zur Globalgruppe hinzufügen
|
||||
foreach ($user in $users) {
|
||||
if (-not (Get-ADGroupMember -Identity $gg -Recursive | Where-Object { $_.SamAccountName -eq $user })) {
|
||||
Add-ADGroupMember -Identity $gg -Members $user
|
||||
Write-Output "Benutzer $user wurde der Globalgruppe $gg hinzugefügt."
|
||||
} else {
|
||||
Write-Output "Benutzer $user ist bereits Mitglied der Globalgruppe $gg."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[System.Windows.Forms.MessageBox]::Show("Globalgruppe $gg wurde den Gruppen $($dl -join ', ') hinzugefügt.")
|
||||
[System.Windows.Forms.MessageBox]::Show("Globalgruppe $gg wurde den Gruppen $($dl -join ', ') hinzugefügt und Benutzer $($users -join ', ') zugeordnet.")
|
||||
})
|
||||
$form.Controls.Add($okButton)
|
||||
|
||||
# Beenden-Button
|
||||
$exitButton = New-Object System.Windows.Forms.Button
|
||||
$exitButton.Text = "Beenden"
|
||||
$exitButton.Location = New-Object System.Drawing.Point(320, 300)
|
||||
$exitButton.Size = New-Object System.Drawing.Size(100, 30)
|
||||
$exitButton.Location = New-Object System.Drawing.Point(400, 265)
|
||||
$exitButton.Size = New-Object System.Drawing.Size(380, 30)
|
||||
$exitButton.Add_Click({
|
||||
$form.Close()
|
||||
})
|
||||
Reference in New Issue
Block a user