scripts/ad_user_groups.ps1 aktualisiert
This commit is contained in:
@@ -2,16 +2,35 @@
|
||||
$csvPath = "path\to\your\csvfile.csv"
|
||||
$csvData = Import-Csv -Path $csvPath
|
||||
|
||||
# Masterzeile extrahieren
|
||||
$masterRow = $csvData | Where-Object { $_.Type -eq 'Master' }
|
||||
|
||||
# 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 'ServiceAccount') {
|
||||
# Name aus FirstName und LastName generieren, falls nicht vorhanden
|
||||
$name = if ($row.Name) { $row.Name } else { $row.FirstName + " " + $row.LastName }
|
||||
|
||||
# Erstellung des Benutzers
|
||||
New-ADUser -Name $row.Name -GivenName $row.FirstName -Surname $row.LastName -SamAccountName $row.SAMAccountName -UserPrincipalName $row.UPN -Path $row.OU -AccountPassword (ConvertTo-SecureString $row.Password -AsPlainText -Force) -Enabled $true
|
||||
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
|
||||
|
||||
# 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 {
|
||||
New-ADGroup -Name $row.Group -GroupScope Global -Path $row.GroupOU
|
||||
New-ADGroup -Name $row.Group -GroupScope Global -Path "OU=$ou,DC=$dc"
|
||||
Add-ADGroupMember -Identity $row.Group -Members $row.SAMAccountName
|
||||
}
|
||||
} elseif ($row.Type -eq 'Group') {
|
||||
# Gruppe anlegen
|
||||
if (-not (Get-ADGroup -Filter "Name -eq '$($row.Group)'")) {
|
||||
New-ADGroup -Name $row.Group -GroupScope Global -Path $path
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user