Untitled

 avatar
unknown
plain_text
25 days ago
2.3 kB
2
Indexable
$CSV = Import-Csv -Path "D:\stockdemo\Desktop\ob_1858c2.csv" -Delimiter ";"
$OU = "AD-SIEGE"
	# Creation de chaque OU avant de créer les utilisateurs
    New-ADOrganizationalUnit -Name $OU -Path "OU=AD-SIEGE,DC=HORIZON,DC=LOCAL" -ProtectedFromAccidentalDeletion $false -ErrorAction SilentlyContinue

# Tracé du tableau
Write-Host ("{0,-15} {1,-15} {2,-15} {3,-25}" -f "Nom", "Prénom", "Département", "Identifiant")
Write-Host ("{0,-15} {1,-15} {2,-15} {3,-25}" -f "---------------", "---------------", "---------------", "---------------")

foreach ($ligne in $CSV)
{
    # Fonction pour retirer les accents et caractères spéciaux
    function Remove-Accents {
    param([string]$InputString)
    $bytes = [Text.Encoding]::GetEncoding("iso-8859-8").GetBytes($InputString)
    return [Text.Encoding]::UTF8.GetString($bytes)
}

    # Mise en majuscule de la première lettre de chaque mot
    function Capitalize($string) {
        return -join ($string.ToLower() -split ' ' | ForEach-Object { $_.Substring(0,1).ToUpper() + $_.Substring(1) })
    }

    # Récupération du nom, prénom et département de chaque utilisateur 
    $nom= Capitalize $ligne.NOM
    $prenom= Capitalize $ligne.PRENOM
    $OU=$ligne.DEPARTEMENT

    # Création de la colonne Identifiant, normalisation des textes et on enlève les accents
    $nomSansAccent = Remove-Accents $nom
    $prenomSansAccent = Remove-Accents $prenom
    $initialePrenom = $prenom.Substring(0,1).ToUpper()
    $identifiant = "$initialePrenom.$($nomSansAccent -replace '[^a-zA-Z]','')"

    # Ecriture des résultats du tri au format tableau
    Write-Host ("{0,-15} {1,-15} {2,-15} {3,-25}" -f $nom, $prenom, $OU, $identifiant)
	
	#Vérification qu'il n'y ait pas de doublon avant de créer un nouvel utilisateur
	if (-not (Get-ADUser -Filter {SamAccountName -eq $identifiant})) 
	{
	
		# Creation de chaque utilisateur en fonction du tableau ainsi que les adresses mail
		New-ADUser -Name "$($prenom) $($nom)" -Surname "$($nom)" -GivenName "$($prenom)" -SamAccountName "$($identifiant)" -UserPrincipalName "$($prenomSansAccent).$($nomSansAccent)@HORIZON.LOCAL" -path "OU=$($OU),OU=AD-SIEGE,DC=HORIZON,DC=LOCAL" -AccountPassword(ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force) -Enabled $true -ChangePasswordAtLogon $false -ErrorAction SilentlyContinue
	
	}
}
Leave a Comment