Untitled
unknown
plain_text
a year ago
1.4 kB
7
Indexable
# Charger le module Active Directory
Import-Module ActiveDirectory
# Définir les paramètres de base
$CSVFile = "C:\Users\Administrateur\Documents\user_data.csv"
$ParentOUPath = "OU=Siège,DC=HORIZON,DC=LOCAL"
# Importer le fichier CSV avec encodage UTF8
if (-not (Test-Path $CSVFile)) { Write-Error "Fichier CSV introuvable : $CSVFile"; return }
$CSVData = Import-CSV -Path $CSVFile -Delimiter ";" -Encoding UTF8
# Créer l'UO parent "Siège" si elle n'existe pas
if (-not (Get-ADOrganizationalUnit -Filter { DistinguishedName -eq $ParentOUPath })) {
New-ADOrganizationalUnit -Name "Siège" -Path "DC=HORIZON,DC=LOCAL"
}
# Ajouter utilisateurs et UO
foreach ($Utilisateur in $CSVData) {
$DepartmentOU = "OU=$($Utilisateur.DEPARTEMENT),$ParentOUPath"
# Créer l'UO département si elle n'existe pas
if (-not (Get-ADOrganizationalUnit -Filter { DistinguishedName -eq $DepartmentOU })) {
New-ADOrganizationalUnit -Name $Utilisateur.DEPARTEMENT -Path $ParentOUPath
}
# Créer l'utilisateur
$Login = "$($Utilisateur.PRENOM).$($Utilisateur.NOM)"
if (-not (Get-ADUser -Filter { SamAccountName -eq $Login })) {
New-ADUser -Name "$($Utilisateur.NOM) $($Utilisateur.PRENOM)" -SamAccountName $Login -Path $DepartmentOU `
-AccountPassword (ConvertTo-SecureString "P@55w0rd" -AsPlainText -Force) -ChangePasswordAtLogon $true -Enabled $true
}
}
Editor is loading...
Leave a Comment