Untitled
TssrNetro
plain_text
9 months ago
4.4 kB
7
Indexable
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
Import-Module ActiveDirectory
$domain = "ComputeSys.lan"
$domainPath = "DC=ComputeSys,DC=lan"
$siegePath = "OU=SIEGE,$domainPath"
$csvPath = "C:\Users\Administrateur\Documents\AD.csv"
# Vérification du fichier CSV
if (-not (Test-Path $csvPath)) {
Write-Host "ERREUR : Fichier $csvPath introuvable." -ForegroundColor Red
exit
}
# Mapping groupe -> OU avec validation
$groupToOUMap = @{
"GG-DIRECTION" = "DIRECTION"
"GG-SECRETARIAT-DIRECTION" = "DIRECTION"
"GG-AVANT-VENTES" = "COMMERCIAUX" # Correction ici
"GG-VENTE" = "COMMERCIAUX" # Correction ici
"GG-COMPTABILITE" = "COMPTABILITE"
"GG-ADMINISTRATEUR" = "ADMINISTRATEUR"
"GG-SUPPORT-01" = "SUPPORT-01"
"GG-SUPPORT-02" = "SUPPORT-02"
}
# Création des UOs
$requiredOUs = @("DIRECTION", "COMMERCIAUX", "COMPTABILITE", "ADMINISTRATEUR", "SUPPORT-01", "SUPPORT-02", "DL")
foreach ($ou in $requiredOUs) {
$targetOU = "OU=$ou,$siegePath"
if (-not (Get-ADOrganizationalUnit -Filter "Name -eq '$ou'" -SearchBase $siegePath -ErrorAction SilentlyContinue)) {
New-ADOrganizationalUnit -Name $ou -Path $siegePath
Write-Host "[+] UO créée : $ou" -ForegroundColor Green
} else {
Write-Host "[ ] UO existe déjà : $ou" -ForegroundColor DarkGray
}
}
# Traitement des utilisateurs
Import-Csv -Path $csvPath -Delimiter ";" -Encoding UTF8 | ForEach-Object {
try {
$entry = $_
Write-Host "`nTraitement de : $($entry.prenom) $($entry.nom)" -ForegroundColor Cyan
# Validation groupe
if (-not $groupToOUMap.ContainsKey($entry.groupe)) {
Write-Host "ERREUR : Groupe '$($entry.groupe)' non valide." -ForegroundColor Red
return
}
# Détermination OU
$ouName = $groupToOUMap[$entry.groupe]
$targetOU = "OU=$ouName,$siegePath"
Write-Host "|-> Groupe cible : $($entry.groupe) dans $ouName"
# Création groupe
if (-not (Get-ADGroup -Filter "Name -eq '$($entry.groupe)'" -SearchBase $targetOU -ErrorAction SilentlyContinue)) {
New-ADGroup -Name $entry.groupe -Path $targetOU -GroupScope Global -GroupCategory Security
Write-Host "|-> [+] Groupe créé : $($entry.groupe)" -ForegroundColor Green
} else {
Write-Host "|-> [ ] Groupe existe déjà : $($entry.groupe)" -ForegroundColor DarkGray
}
# Génération username
$username = ($entry.prenom + "." + $entry.nom).Trim('.') -replace '[^a-zA-Z0-9\.]','' -replace '\.{2,}','.' -replace '^\.|\.$',''
$username = $username.ToLower()
# Création utilisateur
if (-not (Get-ADUser -Filter "SamAccountName -eq '$username'" -ErrorAction SilentlyContinue)) {
$userParams = @{
SamAccountName = $username
Name = "$($entry.prenom) $($entry.nom)".Trim()
GivenName = $entry.prenom
Surname = $entry.nom
UserPrincipalName = "$username@$domain"
AccountPassword = ConvertTo-SecureString "P@ssw0rd123!" -AsPlainText -Force
Enabled = $true
Path = $targetOU
ChangePasswordAtLogon = $true
}
$newUser = New-ADUser @userParams -PassThru
Write-Host "|-> [+] Utilisateur créé : $username" -ForegroundColor Green
} else {
Write-Host "|-> [ ] Utilisateur existe déjà : $username" -ForegroundColor DarkGray
$newUser = Get-ADUser -Filter "SamAccountName -eq '$username'"
}
# Ajout au groupe
if ($newUser) {
try {
Add-ADGroupMember -Identity $entry.groupe -Members $newUser -ErrorAction Stop
Write-Host "|-> [+] Utilisateur ajouté au groupe : $($entry.groupe)" -ForegroundColor Green
} catch {
Write-Host "|-> ERREUR : Échec de l'ajout au groupe [$($entry.groupe)] pour [$username] : $_" -ForegroundColor Red
}
}
}
catch {
Write-Host "|-> ERREUR GLOBALE : $_" | Out-File "$env:TEMP\AD_Errors.log" -Append
}
}
Write-Host "`nTraitement terminé. Vérifiez les messages ci-dessus." -ForegroundColor Cyan
Editor is loading...
Leave a Comment