powershell

This commit is contained in:
2025-11-05 09:59:26 +01:00
parent 016596ccfb
commit 721c595e4b
3 changed files with 140 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,115 @@
<#
.SYNOPSIS
Adds new Active Directory users from a CSV file.
.DESCRIPTION
Reads a CSV with user details and creates corresponding AD accounts.
.NOTES
Requires RSAT / ActiveDirectory module.
#>
$base = "DC=company,DC=local"
# Path to CSV file
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$csvPath = Join-Path $scriptPath "users.csv"
# 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
}
# 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" -ForegroundColor Cyan
}
}
foreach ($user in $users) {
$FirstName = $user.FirstName
$LastName = $user.LastName
$Username = $user.Username
$OU = $user.OU
$Password = (ConvertTo-SecureString $user.Password -AsPlainText -Force)
$Department = $user.Department
$Title = $user.Title
$DisplayName = "$FirstName $LastName"
$Email = "$Username@example.com"
# Check if user already exists
if (Get-ADUser -Filter {SamAccountName -eq $Username}) {
Write-Host "User $Username already exists, skipping..." -ForegroundColor Yellow
continue
}
# 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 `
-Name $DisplayName `
-GivenName $FirstName `
-Surname $LastName `
-DisplayName $DisplayName `
-Path $ouPath `
-Department $Department `
-Title $Title `
-AccountPassword $Password `
-Enabled $true `
-ChangePasswordAtLogon $true `
-ErrorAction Stop
Write-Host "Successfully created user: $DisplayName ($Username)" -ForegroundColor Green
}
catch {
Write-Host "Failed to create user $Username" -ForegroundColor Red
Write-Host "Error details: $_" -ForegroundColor Red
Write-Host "Stack trace: $($_.ScriptStackTrace)" -ForegroundColor Red
}
}
Write-Host "User import complete."

View File

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