Untitled
TssrNetro
plain_text
14 days ago
4.1 kB
3
Indexable
# Correction de l'encodage de la console [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 # Importer le module Active Directory Import-Module ActiveDirectory # Chemins LDAP $domain = "ComputeSys.lan" $domainPath = "DC=ComputeSys,DC=lan" $siegePath = "OU=SIEGE,$domainPath" # Chemin du fichier CSV $csvPath = "C:\Users\Administrateur\Documents\AD.csv" # Mapping des groupes vers leurs UOs $groupToOUMap = @{ "GG-DIRECTION" = "DIRECTION" "GG-SECRETARIAT-DIRECTION" = "DIRECTION" "GG-AVANT-VENTES" = "COMMERCIEUX" "GG-VENTE" = "COMMERCIEUX" "GG-COMPTABILITE" = "COMPTABILITE" "GG-ADMINISTRATEUR" = "ADMINISTRATEUR" "GG-SUPPORT-01" = "SUPPORT-01" "GG-SUPPORT-02" = "SUPPORT-02" } # Vérification du fichier CSV if (-not (Test-Path $csvPath)) { Write-Host "ERREUR : Le fichier $csvPath est introuvable." -ForegroundColor Red exit } # Création des UOs principales si nécessaire $requiredOUs = @("DIRECTION", "COMMERCIEUX", "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 '$ou' créée sous 'SIEGE'." -ForegroundColor Green } } # Traitement des entrées CSV Import-Csv -Path $csvPath -Delimiter ";" -Encoding UTF8 | ForEach-Object { try { # Vérification des données obligatoires if ([string]::IsNullOrWhiteSpace($_.prenom) -or [string]::IsNullOrWhiteSpace($_.groupe)) { Write-Host "Données manquantes pour : $($_.prenom)" -ForegroundColor Yellow return } # Détermination de l'UO cible if (-not $groupToOUMap.ContainsKey($_.groupe)) { Write-Host "Aucun mapping trouvé pour le groupe $($_.groupe)" -ForegroundColor Red return } $ouName = $groupToOUMap[$_.groupe] $targetOU = "OU=$ouName,$siegePath" # Création du groupe s'il n'existe pas if (-not (Get-ADGroup -Filter "Name -eq '$($_.groupe)'" -SearchBase $targetOU -ErrorAction SilentlyContinue)) { New-ADGroup -Name $_.groupe ` -SamAccountName $_.groupe ` -GroupCategory Security ` -GroupScope Global ` -Path $targetOU Write-Host "Groupe [$($_.groupe)] créé dans [$ouName]" -ForegroundColor Cyan } # Génération du nom d'utilisateur $firstName = $_.prenom.Trim().Normalize("FormD") -replace '\p{M}', '' $lastName = if ($_.nom) { $_.nom.Trim().Normalize("FormD") -replace '\p{M}', '' } else { "" } $username = "$($firstName).$($lastName)".ToLower() -replace '[^a-z0-9.]' # Création de l'utilisateur if (-not (Get-ADUser -Filter "SamAccountName -eq '$username'" -ErrorAction SilentlyContinue)) { $userParams = @{ GivenName = $firstName Surname = $lastName Name = "$firstName $lastName" SamAccountName = $username UserPrincipalName = "$username@$domain" AccountPassword = ConvertTo-SecureString "P@ssw0rd123!" -AsPlainText -Force Enabled = $true Path = $targetOU ChangePasswordAtLogon = $true } $newUser = New-ADUser @userParams -PassThru Add-ADGroupMember -Identity $_.groupe -Members $newUser Write-Host "Utilisateur [$username] créé dans [$ouName]" -ForegroundColor Green } } catch { Write-Host "ERREUR avec $($_.prenom) : $_" -ForegroundColor Red } } Write-Host "`nTraitement terminé avec succès !" -ForegroundColor Green
Editor is loading...
Leave a Comment