scripts/old/ad_user_groups.ps1 aktualisiert
This commit is contained in:
58
scripts/old/ad_user_groups.ps1
Normal file
58
scripts/old/ad_user_groups.ps1
Normal file
@@ -0,0 +1,58 @@
|
||||
# Import der CSV-Datei
|
||||
$csvPath = "path\to\your\csvfile.csv"
|
||||
$csvData = Import-Csv -Path $csvPath
|
||||
|
||||
# Masterzeile extrahieren
|
||||
$masterRow = $csvData | Where-Object { $_.Type -eq 'Master' }
|
||||
|
||||
# Listen für vorhandene Benutzer und Gruppen
|
||||
$existingUsers = @()
|
||||
$existingGroups = @()
|
||||
|
||||
# Durchlaufen jeder Zeile der CSV-Datei
|
||||
foreach ($row in $csvData) {
|
||||
if ($row.Type -ne 'Master') {
|
||||
$ou = if ($row.OU) { $row.OU } else { $masterRow.OU }
|
||||
$dc = if ($row.DC) { $row.DC } else { $masterRow.DC }
|
||||
$path = "OU=$ou,DC=$dc"
|
||||
|
||||
if ($row.Type -eq 'User' -or $row.Type -eq 'SA') {
|
||||
# Name aus FirstName und LastName generieren, falls nicht vorhanden
|
||||
$name = if ($row.Name) { $row.Name } else { $row.FirstName + " " + $row.LastName }
|
||||
|
||||
# Überprüfen, ob der Benutzer bereits existiert
|
||||
if (Get-ADUser -Filter "SamAccountName -eq '$($row.SAMAccountName)'") {
|
||||
$existingUsers += $row.SAMAccountName
|
||||
} else {
|
||||
# Erstellung des Benutzers
|
||||
New-ADUser -Name $name -GivenName $row.FirstName -Surname $row.LastName -SamAccountName $row.SAMAccountName -UserPrincipalName $row.UPN -Path $path -AccountPassword (ConvertTo-SecureString $row.Password -AsPlainText -Force) -Enabled $true
|
||||
|
||||
# Gruppenmitgliedschaft prüfen und hinzufügen
|
||||
$groups = $row.Group -split ','
|
||||
foreach ($group in $groups) {
|
||||
if (Get-ADGroup -Filter "Name -eq '$group'") {
|
||||
Add-ADGroupMember -Identity $group -Members $row.SAMAccountName
|
||||
} else {
|
||||
New-ADGroup -Name $group -GroupScope Global -Path "OU=$ou,DC=$dc"
|
||||
Add-ADGroupMember -Identity $group -Members $row.SAMAccountName
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($row.Type -eq 'Group') {
|
||||
# Überprüfen, ob die Gruppe bereits existiert
|
||||
if (Get-ADGroup -Filter "Name -eq '$($row.Group)'") {
|
||||
$existingGroups += $row.Group
|
||||
} else {
|
||||
# Gruppe anlegen
|
||||
New-ADGroup -Name $row.Group -GroupScope Global -Path $path
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Zusammenfassung der vorhandenen Benutzer und Gruppen
|
||||
Write-Host "`nBereits vorhandene Benutzer und Gruppen:" -ForegroundColor Red
|
||||
Write-Host "Benutzer:" -ForegroundColor Red
|
||||
$existingUsers | ForEach-Object { Write-Host $_ -ForegroundColor Red }
|
||||
Write-Host "Gruppen:" -ForegroundColor Red
|
||||
$existingGroups | ForEach-Object { Write-Host $_ -ForegroundColor Red }
|
||||
Reference in New Issue
Block a user