scripts/ad_users_sa_groups_window.ps1 hinzugefügt
This commit is contained in:
82
scripts/ad_users_sa_groups_window.ps1
Normal file
82
scripts/ad_users_sa_groups_window.ps1
Normal file
@@ -0,0 +1,82 @@
|
||||
# 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 = "Benutzer- und Gruppenerstellung"
|
||||
$form.Size = New-Object System.Drawing.Size(500, 600)
|
||||
$form.StartPosition = "CenterScreen"
|
||||
|
||||
# Labels und Textboxen erstellen
|
||||
$labels = @("Type", "Name", "FirstName", "LastName", "SAMAccountName", "UPN", "OU", "DC", "Password", "Group", "GroupOU")
|
||||
$positions = 20
|
||||
$textBoxes = @{}
|
||||
|
||||
foreach ($label in $labels) {
|
||||
$labelControl = New-Object System.Windows.Forms.Label
|
||||
$labelControl.Text = $label
|
||||
$labelControl.Size = New-Object System.Drawing.Size(120, 20)
|
||||
$labelControl.Location = New-Object System.Drawing.Point(20, $positions)
|
||||
$form.Controls.Add($labelControl)
|
||||
|
||||
$textBox = New-Object System.Windows.Forms.TextBox
|
||||
$textBox.Size = New-Object System.Drawing.Size(300, 20)
|
||||
$textBox.Location = New-Object System.Drawing.Point(150, $positions)
|
||||
$form.Controls.Add($textBox)
|
||||
$textBoxes[$label] = $textBox
|
||||
|
||||
$positions += 30
|
||||
}
|
||||
|
||||
# OK-Button
|
||||
$okButton = New-Object System.Windows.Forms.Button
|
||||
$okButton.Text = "OK"
|
||||
$okButton.Location = New-Object System.Drawing.Point(200, $positions)
|
||||
$okButton.Add_Click({
|
||||
$form.Close()
|
||||
})
|
||||
$form.Controls.Add($okButton)
|
||||
|
||||
# Formular anzeigen
|
||||
$form.ShowDialog()
|
||||
|
||||
# Eingabedaten extrahieren
|
||||
$data = @{
|
||||
Type = $textBoxes["Type"].Text
|
||||
Name = $textBoxes["Name"].Text
|
||||
FirstName = $textBoxes["FirstName"].Text
|
||||
LastName = $textBoxes["LastName"].Text
|
||||
SAMAccountName = $textBoxes["SAMAccountName"].Text
|
||||
UPN = $textBoxes["UPN"].Text
|
||||
OU = $textBoxes["OU"].Text
|
||||
DC = $textBoxes["DC"].Text
|
||||
Password = $textBoxes["Password"].Text
|
||||
Group = $textBoxes["Group"].Text
|
||||
GroupOU = $textBoxes["GroupOU"].Text
|
||||
}
|
||||
|
||||
# Überprüfen, ob die OU und DC Werte haben
|
||||
if (-not $data.OU) { $data.OU = "DefaultOU" }
|
||||
if (-not $data.DC) { $data.DC = "DefaultDC" }
|
||||
|
||||
# Benutzer oder Gruppe erstellen basierend auf dem Typ
|
||||
if ($data.Type -eq 'User' -or $data.Type -eq 'SA') {
|
||||
$name = if ($data.Name) { $data.Name } else { $data.FirstName + " " + $data.LastName }
|
||||
if (-not (Get-ADUser -Filter "SamAccountName -eq '$($data.SAMAccountName)'")) {
|
||||
New-ADUser -Name $name -GivenName $data.FirstName -Surname $data.LastName -SamAccountName $data.SAMAccountName -UserPrincipalName $data.UPN -Path "OU=$($data.OU),DC=$($data.DC)" -AccountPassword (ConvertTo-SecureString $data.Password -AsPlainText -Force) -Enabled $true
|
||||
$groups = $data.Group -split ','
|
||||
foreach ($group in $groups) {
|
||||
if (Get-ADGroup -Filter "Name -eq '$group'") {
|
||||
Add-ADGroupMember -Identity $group -Members $data.SAMAccountName
|
||||
} else {
|
||||
New-ADGroup -Name $group -GroupScope Global -Path "OU=$($data.GroupOU),DC=$($data.DC)"
|
||||
Add-ADGroupMember -Identity $group -Members $data.SAMAccountName
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($data.Type -eq 'Group') {
|
||||
if (-not (Get-ADGroup -Filter "Name -eq '$($data.Group)'")) {
|
||||
New-ADGroup -Name $data.Group -GroupScope Global -Path "OU=$($data.GroupOU),DC=$($data.DC)"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user