scripts/ad_user_groups.ps1 aktualisiert
This commit is contained in:
@@ -5,6 +5,10 @@ $csvData = Import-Csv -Path $csvPath
|
|||||||
# Masterzeile extrahieren
|
# Masterzeile extrahieren
|
||||||
$masterRow = $csvData | Where-Object { $_.Type -eq 'Master' }
|
$masterRow = $csvData | Where-Object { $_.Type -eq 'Master' }
|
||||||
|
|
||||||
|
# Listen für vorhandene Benutzer und Gruppen
|
||||||
|
$existingUsers = @()
|
||||||
|
$existingGroups = @()
|
||||||
|
|
||||||
# Durchlaufen jeder Zeile der CSV-Datei
|
# Durchlaufen jeder Zeile der CSV-Datei
|
||||||
foreach ($row in $csvData) {
|
foreach ($row in $csvData) {
|
||||||
if ($row.Type -ne 'Master') {
|
if ($row.Type -ne 'Master') {
|
||||||
@@ -12,25 +16,43 @@ foreach ($row in $csvData) {
|
|||||||
$dc = if ($row.DC) { $row.DC } else { $masterRow.DC }
|
$dc = if ($row.DC) { $row.DC } else { $masterRow.DC }
|
||||||
$path = "OU=$ou,DC=$dc"
|
$path = "OU=$ou,DC=$dc"
|
||||||
|
|
||||||
if ($row.Type -eq 'User' -or $row.Type -eq 'ServiceAccount') {
|
if ($row.Type -eq 'User' -or $row.Type -eq 'SA') {
|
||||||
# Name aus FirstName und LastName generieren, falls nicht vorhanden
|
# Name aus FirstName und LastName generieren, falls nicht vorhanden
|
||||||
$name = if ($row.Name) { $row.Name } else { $row.FirstName + " " + $row.LastName }
|
$name = if ($row.Name) { $row.Name } else { $row.FirstName + " " + $row.LastName }
|
||||||
|
|
||||||
# Erstellung des Benutzers
|
# Überprüfen, ob der Benutzer bereits existiert
|
||||||
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
|
if (Get-ADUser -Filter "SamAccountName -eq '$($row.SAMAccountName)'") {
|
||||||
|
$existingUsers += $row.SAMAccountName
|
||||||
# Wenn die Gruppe existiert, füge den Benutzer hinzu, andernfalls erstelle die Gruppe und füge den Benutzer hinzu
|
|
||||||
if (Get-ADGroup -Filter "Name -eq '$($row.Group)'") {
|
|
||||||
Add-ADGroupMember -Identity $row.Group -Members $row.SAMAccountName
|
|
||||||
} else {
|
} else {
|
||||||
New-ADGroup -Name $row.Group -GroupScope Global -Path "OU=$ou,DC=$dc"
|
# Erstellung des Benutzers
|
||||||
Add-ADGroupMember -Identity $row.Group -Members $row.SAMAccountName
|
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') {
|
} elseif ($row.Type -eq 'Group') {
|
||||||
# Gruppe anlegen
|
# Überprüfen, ob die Gruppe bereits existiert
|
||||||
if (-not (Get-ADGroup -Filter "Name -eq '$($row.Group)'")) {
|
if (Get-ADGroup -Filter "Name -eq '$($row.Group)'") {
|
||||||
|
$existingGroups += $row.Group
|
||||||
|
} else {
|
||||||
|
# Gruppe anlegen
|
||||||
New-ADGroup -Name $row.Group -GroupScope Global -Path $path
|
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