fixed csv layout for OUs

This commit is contained in:
2025-11-05 09:52:33 +01:00
parent 576bad5fcd
commit 97db8188b0
2 changed files with 86 additions and 62 deletions

View File

@@ -6,38 +6,57 @@
.NOTES
Requires RSAT / ActiveDirectory module.
#>
Import-Module ActiveDirectory
# Path to CSV file
$csvPath = ".\users.csv"
# Import from CSV
$users = Import-Csv -Path $csvPath
$base = "DC=company,DC=local"
# Path to CSV file
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$csvPath = Join-Path $scriptPath "users.csv"
$ous = @(
"OU=Employees,$base",
"OU=Directie,OU=Employees,$base",
"OU=HR,OU=Employees,$base",
"OU=BeheerOntwikkeling,OU=Employees,$base",
"OU=Duitsland,OU=Employees,$base",
"OU=Benelux,OU=Employees,$base",
"OU=ZuidEuropa,OU=Employees,$base",
"OU=SalesMarketing,OU=Employees,$base",
"OU=Operations,OU=Employees,$base",
"OU=FoodBeverages,OU=Employees,$base",
"OU=Hospitality,OU=Employees,$base",
"OU=ICT,OU=Employees,$base",
"OU=Finance,OU=Employees,$base"
)
# Try to import Active Directory module
try {
Import-Module ActiveDirectory -ErrorAction Stop
} catch {
Write-Error "Failed to import Active Directory module. Please ensure RSAT tools are installed and you have administrative privileges."
Write-Error "Error: $_"
exit 1
}
foreach ($ou in $ous) {
if (-not (Get-ADOrganizationalUnit -LDAPFilter "(distinguishedName=$ou)" -ErrorAction SilentlyContinue)) {
New-ADOrganizationalUnit -Name ($ou.Split(",")[0].Replace("OU=","")) -Path ($ou.Substring($ou.IndexOf(",")+1))
Write-Host "Created OU: $ou"
# Import from CSV
Write-Host "Reading users from: $csvPath"
try {
$users = Import-Csv -Path $csvPath
Write-Host "Successfully loaded $($users.Count) users from CSV"
} catch {
Write-Error "Failed to read CSV file: $_"
exit 1
}
# Make sure Employees OU exists
if (-not (Get-ADOrganizationalUnit -Filter {DistinguishedName -eq "OU=Employees,$base"} -ErrorAction SilentlyContinue)) {
try {
New-ADOrganizationalUnit -Name "Employees" -Path $base
Write-Host "Created base Employees OU" -ForegroundColor Green
} catch {
Write-Error "Failed to create Employees OU: $_"
exit 1
}
}
# Get unique OUs from CSV file
$requiredOUs = $users | Select-Object -ExpandProperty OU -Unique
# Create each required OU if it doesn't exist
foreach ($ou in $requiredOUs) {
$ouPath = "OU=$ou,OU=Employees,$base"
if (-not (Get-ADOrganizationalUnit -Filter {DistinguishedName -eq $ouPath} -ErrorAction SilentlyContinue)) {
try {
New-ADOrganizationalUnit -Name $ou -Path "OU=Employees,$base"
Write-Host "Created OU: $ou under Employees" -ForegroundColor Green
} catch {
Write-Warning "Failed to create OU: $ou - $_"
}
} else {
Write-Host "OU already exists: $ou"
Write-Host "OU already exists: $ou" -ForegroundColor Cyan
}
}
@@ -60,6 +79,15 @@ foreach ($user in $users) {
# Create the user
try {
$ouPath = "OU=$OU,OU=Employees,$base"
Write-Host "Attempting to create user: $Username in OU: $ouPath" -ForegroundColor Cyan
# Verify OU exists first
if (-not (Get-ADOrganizationalUnit -Filter {DistinguishedName -eq $ouPath} -ErrorAction SilentlyContinue)) {
Write-Host "Error: OU '$ouPath' does not exist!" -ForegroundColor Red
continue
}
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName $Email `
@@ -67,17 +95,20 @@ foreach ($user in $users) {
-GivenName $FirstName `
-Surname $LastName `
-DisplayName $DisplayName `
-Path $OU `
-Path $ouPath `
-Department $Department `
-Title $Title `
-AccountPassword $Password `
-Enabled $true `
-ChangePasswordAtLogon $true
-ChangePasswordAtLogon $true `
-ErrorAction Stop
Write-Host "Created user: $DisplayName ($Username)" -ForegroundColor Green
Write-Host "Successfully created user: $DisplayName ($Username)" -ForegroundColor Green
}
catch {
Write-Host "Failed to create user $Username: $_" -ForegroundColor Red
Write-Host "Failed to create user $Username" -ForegroundColor Red
Write-Host "Error details: $_" -ForegroundColor Red
Write-Host "Stack trace: $($_.ScriptStackTrace)" -ForegroundColor Red
}
}