Untitled
unknown
plain_text
9 months ago
1.9 kB
4
Indexable
$CSVData = Import-Csv -Path "C:\Users\Administrateur\Desktop\Utilisateurs.csv" -Delimiter ";" -Encoding UTF8
#création uo ENTREPRISE
New-ADOrganizationalUnit -Name "ENTREPRISE" -Path "DC=HORIZON,DC=LOCAL"
Write-Output "L'OU ENTREPRISE a été créee."
foreach ($ligne in $CSVData) {
# Utilise Variables utilisateur à partir du CSV
$Prenom = $ligne.Prenom
$Nom = $ligne.Nom
$Login = "$Prenom.$Nom"
$MotDePasse = "P@55w0rd"
$UO = $ligne.departement # La colonne 'departement' correspond à l'UO dans le CSV
# Vérifier si l'UO existe
$UOPath = "OU=$UO,OU=ENTREPRISE,DC=HORIZON,DC=LOCAL"
if (-not (Get-ADOrganizationalUnit -Filter {DistinguishedName -eq $UOPath} -ErrorAction SilentlyContinue)) {
Write-Output "L'UO $UO n'existe pas. Création de l'UO..."
# Créer l'UO si elle n'existe pas
New-ADOrganizationalUnit -Name $UO -Path "OU=ENTREPRISE,DC=HORIZON,DC=LOCAL"
Write-Output "L'UO $UO a été créée."
}
# Vérifier la présence de l'utilisateur dans l'AD
if (Get-ADUser -Filter {SamAccountName -eq $Login}) {
Write-Warning "L'identifiant $Login existe déjà dans l'AD"
} else {
# Créer l'utilisateur dans l'AD, en utilisant l'OU dynamique
New-ADUser -Name "$Prenom $nom" `
-DisplayName "$Prenom $Nom" `
-GivenName $Prenom `
-Surname $Nom `
-SamAccountName $Login `
-UserPrincipalName "$Login@HORIZON.LOCAL" `
-Path $UOPath `
-AccountPassword (ConvertTo-SecureString $MotDePasse -AsPlainText -Force) `
-ChangePasswordAtLogon $true `
-Enabled $true
Write-Output "Création de l'utilisateur : $Login ($Prenom $Nom)"
}
}
Editor is loading...
Leave a Comment