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
}
}

View File

@@ -1,32 +1,25 @@
FirstName,LastName,Username,OU,Password,Department,Title
Henriette,Fonteyn,hfonteyn,"OU=Directie,OU=Employees,DC=company,DC=local","P@ssword123","Directie","Algemeen Directeur"
Frank,Fonteyn,ffonteyn,"OU=Directie,OU=Employees,DC=company,DC=local","P@ssword123","Directie","Financieel Directeur"
Anja,de Groot,adegroot,"OU=Directie,OU=Employees,DC=company,DC=local","P@ssword123","Directie","Directie assistente"
Inge,Winsemius-Schut,iwinsemiusschut,"OU=HR,OU=Employees,DC=company,DC=local","P@ssword123","HR","Hoofd HR"
Antoon,van der Hoeven,avanderhoeven,"OU=BeheerOntwikkeling,OU=Employees,DC=company,DC=local","P@ssword123","Beheer & Ontwikkeling","Directeur Beheer & Ontwikkeling"
Freek,van der Plas,fvanderplas,"OU=BeheerOntwikkeling,OU=Employees,DC=company,DC=local","P@ssword123","Beheer & Ontwikkeling","Beheer & Ontwikkeling"
Fatima,Laroussi,flaroussi,"OU=BeheerOntwikkeling,OU=Employees,DC=company,DC=local","P@ssword123","M&A","M&A"
Norman,Jorgens,njorgens,"OU=Duitsland,OU=Employees,DC=company,DC=local","P@ssword123","Duitsland","Land directeur Duitsland"
Erica,Bessels,ebessels,"OU=Benelux,OU=Employees,DC=company,DC=local","P@ssword123","Benelux","Land directeur Benelux"
JeanJacques,Velo,jvelo,"OU=ZuidEuropa,OU=Employees,DC=company,DC=local","P@ssword123","Zuid-Europa","Land directeur Zuid-Europa"
Jelle,Snelle,jsnelle,"OU=SalesMarketing,OU=Employees,DC=company,DC=local","P@ssword123","Sales & Marketing","Manager Sales & Marketing"
Cheng,Fui,cfui,"OU=SalesMarketing,OU=Employees,DC=company,DC=local","P@ssword123","Sales & Marketing","Sales"
Harrie,Makers,hmakers,"OU=Operations,OU=Employees,DC=company,DC=local","P@ssword123","Operations","Operations Manager"
Eric,de Knutselaar,edeknutselaar,"OU=Operations,OU=Employees,DC=company,DC=local","P@ssword123","Technische Dienst","Technische dienst"
Piet,Poester,ppoester,"OU=Operations,OU=Employees,DC=company,DC=local","P@ssword123","Schoonmaak","Schoonmaak"
Francien,de Kok,fdekok,"OU=FoodBeverages,OU=Employees,DC=company,DC=local","P@ssword123","Food & Beverages","Manager Food & Beverages"
Mohammed,Ozturk,mozturk,"OU=Hospitality,OU=Employees,DC=company,DC=local","P@ssword123","Hospitality","Manager Hospitality"
Stefaan,Vrijsen,svrijsen,"OU=ICT,OU=Employees,DC=company,DC=local","P@ssword123","ICT & Security","ICT & Security Manager"
John,Ntwari,jntwari,"OU=ICT,OU=Employees,DC=company,DC=local","P@ssword123","ICT & Security","Helpdesk"
Frank,Ventiel,fventiel,"OU=ICT,OU=Employees,DC=company,DC=local","P@ssword123","ICT & Security","Systeembeheerder"
Sohail,Sosa,ssosa,"OU=ICT,OU=Employees,DC=company,DC=local","P@ssword123","ICT & Security","Applicatiebeheerder"
Frits,Franken,ffranken,"OU=ICT,OU=Employees,DC=company,DC=local","P@ssword123","ICT & Security","ICT Projectmanagement"
Bernhard,vandenBroek,bvandenbroek,"OU=ICT,OU=Employees,DC=company,DC=local","P@ssword123","ICT & Security","ICT Demandmanagement"
Anke,van Dalen-Schoten,avandalen,"OU=Finance,OU=Employees,DC=company,DC=local","P@ssword123","Finance & Control","Finance & Control Manager"
Henriette,Fonteyn,hfonteyn,"Directie","P@ssword123","Directie","Algemeen Directeur"
Frank,Fonteyn,ffonteyn,"Directie","P@ssword123","Directie","Financieel Directeur"
Anja,de Groot,adegroot,"Directie","P@ssword123","Directie","Directie assistente"
Inge,Winsemius-Schut,iwinsemiusschut,"HR","P@ssword123","HR","Hoofd HR"
Antoon,van der Hoeven,avanderhoeven,"BeheerOntwikkeling","P@ssword123","Beheer & Ontwikkeling","Directeur Beheer & Ontwikkeling"
Freek,van der Plas,fvanderplas,"BeheerOntwikkeling","P@ssword123","Beheer & Ontwikkeling","Beheer & Ontwikkeling"
Fatima,Laroussi,flaroussi,"BeheerOntwikkeling","P@ssword123","M&A","M&A"
Norman,Jorgens,njorgens,"Duitsland","P@ssword123","Duitsland","Land directeur Duitsland"
Erica,Bessels,ebessels,"Benelux","P@ssword123","Benelux","Land directeur Benelux"
JeanJacques,Velo,jvelo,"ZuidEuropa","P@ssword123","Zuid-Europa","Land directeur Zuid-Europa"
Jelle,Snelle,jsnelle,"SalesMarketing","P@ssword123","Sales & Marketing","Manager Sales & Marketing"
Cheng,Fui,cfui,"SalesMarketing","P@ssword123","Sales & Marketing","Sales"
Harrie,Makers,hmakers,"Operations","P@ssword123","Operations","Operations Manager"
Eric,de Knutselaar,edeknutselaar,"Operations","P@ssword123","Technische Dienst","Technische dienst"
Piet,Poester,ppoester,"Operations","P@ssword123","Schoonmaak","Schoonmaak"
Francien,de Kok,fdekok,"FoodBeverages","P@ssword123","Food & Beverages","Manager Food & Beverages"
Mohammed,Ozturk,mozturk,"Hospitality","P@ssword123","Hospitality","Manager Hospitality"
Stefaan,Vrijsen,svrijsen,"ICT","P@ssword123","ICT & Security","ICT & Security Manager"
John,Ntwari,jntwari,"ICT","P@ssword123","ICT & Security","Helpdesk"
Frank,Ventiel,fventiel,"ICT","P@ssword123","ICT & Security","Systeembeheerder"
Sohail,Sosa,ssosa,"ICT","P@ssword123","ICT & Security","Applicatiebeheerder"
Frits,Franken,ffranken,"ICT","P@ssword123","ICT & Security","ICT Projectmanagement"
Bernhard,vandenBroek,bvandenbroek,"ICT","P@ssword123","ICT & Security","ICT Demandmanagement"
Anke,van Dalen-Schoten,avandalen,"Finance","P@ssword123","Finance & Control","Finance & Control Manager"
1 FirstName LastName Username OU Password Department Title
2 Henriette Fonteyn hfonteyn OU=Directie,OU=Employees,DC=company,DC=local Directie P@ssword123 Directie Algemeen Directeur
3 Frank Fonteyn ffonteyn OU=Directie,OU=Employees,DC=company,DC=local Directie P@ssword123 Directie Financieel Directeur
4 Anja de Groot adegroot OU=Directie,OU=Employees,DC=company,DC=local Directie P@ssword123 Directie Directie assistente
5 Inge Winsemius-Schut iwinsemiusschut OU=HR,OU=Employees,DC=company,DC=local HR P@ssword123 HR Hoofd HR
6 Antoon van der Hoeven avanderhoeven OU=BeheerOntwikkeling,OU=Employees,DC=company,DC=local BeheerOntwikkeling P@ssword123 Beheer & Ontwikkeling Directeur Beheer & Ontwikkeling
7 Freek van der Plas fvanderplas OU=BeheerOntwikkeling,OU=Employees,DC=company,DC=local BeheerOntwikkeling P@ssword123 Beheer & Ontwikkeling Beheer & Ontwikkeling
8 Fatima Laroussi flaroussi OU=BeheerOntwikkeling,OU=Employees,DC=company,DC=local BeheerOntwikkeling P@ssword123 M&A M&A
9 Norman Jorgens njorgens OU=Duitsland,OU=Employees,DC=company,DC=local Duitsland P@ssword123 Duitsland Land directeur Duitsland
10 Erica Bessels ebessels OU=Benelux,OU=Employees,DC=company,DC=local Benelux P@ssword123 Benelux Land directeur Benelux
11 JeanJacques Velo jvelo OU=ZuidEuropa,OU=Employees,DC=company,DC=local ZuidEuropa P@ssword123 Zuid-Europa Land directeur Zuid-Europa
12 Jelle Snelle jsnelle OU=SalesMarketing,OU=Employees,DC=company,DC=local SalesMarketing P@ssword123 Sales & Marketing Manager Sales & Marketing
13 Cheng Fui cfui OU=SalesMarketing,OU=Employees,DC=company,DC=local SalesMarketing P@ssword123 Sales & Marketing Sales
14 Harrie Makers hmakers OU=Operations,OU=Employees,DC=company,DC=local Operations P@ssword123 Operations Operations Manager
15 Eric de Knutselaar edeknutselaar OU=Operations,OU=Employees,DC=company,DC=local Operations P@ssword123 Technische Dienst Technische dienst
16 Piet Poester ppoester OU=Operations,OU=Employees,DC=company,DC=local Operations P@ssword123 Schoonmaak Schoonmaak
17 Francien de Kok fdekok OU=FoodBeverages,OU=Employees,DC=company,DC=local FoodBeverages P@ssword123 Food & Beverages Manager Food & Beverages
18 Mohammed Ozturk mozturk OU=Hospitality,OU=Employees,DC=company,DC=local Hospitality P@ssword123 Hospitality Manager Hospitality
19 Stefaan Vrijsen svrijsen OU=ICT,OU=Employees,DC=company,DC=local ICT P@ssword123 ICT & Security ICT & Security Manager
20 John Ntwari jntwari OU=ICT,OU=Employees,DC=company,DC=local ICT P@ssword123 ICT & Security Helpdesk
21 Frank Ventiel fventiel OU=ICT,OU=Employees,DC=company,DC=local ICT P@ssword123 ICT & Security Systeembeheerder
22 Sohail Sosa ssosa OU=ICT,OU=Employees,DC=company,DC=local ICT P@ssword123 ICT & Security Applicatiebeheerder
23 Frits Franken ffranken OU=ICT,OU=Employees,DC=company,DC=local ICT P@ssword123 ICT & Security ICT Projectmanagement
24 Bernhard vandenBroek bvandenbroek OU=ICT,OU=Employees,DC=company,DC=local ICT P@ssword123 ICT & Security ICT Demandmanagement
25 Anke van Dalen-Schoten avandalen OU=Finance,OU=Employees,DC=company,DC=local Finance P@ssword123 Finance & Control Finance & Control Manager