Sort users in active directory using powershell and csv

Welcome Forums General PowerShell Q&A Sort users in active directory using powershell and csv

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 week, 6 days ago.

  • Author
    Posts
  • #181233

    Participant
    Topics: 1
    Replies: 0
    Points: -23
    Rank: Member

    Hi , I have those users, from a csv script, and I sorted them all in OU (related as office in my script column). But in my OU there are others OU(related as departement) and I need to classify them into those to!

     

    So in my script "Comptabilite" refeirs as $office and "Reglements locaux" as $dept.

    To sorted the users that belong in "Comptabilite", "Financements","Gestion des contrats" and "Unite Siege" ($office), I did this script and it was sucessful

    foreach ($user in $users){
    
    $name = $user.firstName + " " + $user.lastName
    
    $fname = $user.firstName
    
    $lname = $user.lastName
    
    $login = $user.firstName + "." + $user.lastName
    
    Uoffice = $user.office
    
    $Upassword = $user.password
    
    $dept = $user.department
    
    
    switch($user.office){
    
    "Comptabilite" {$office = "OU=Comptabilite,OU=Exercise1,DC=college,DC=local"}
    
    "Unite Siege" {$office = "OU=Unite Siege,OU=Exercise1,DC=college,DC=local"}
    
    "Financements" {$office = "OU=Financements,OU=Exercise1,DC=college,DC=local"}
    
    "Gestion des contrats" {$office = "OU=Gestion des contrats,OU=Exercises,DC=college,DC=local"}
    
    default {$office = $null}
    
    }  try {  New-ADUser -Name $name -SamAccountName $login -UserPrincipalName $login -DisplayName $name -GivenName $fname -Surname $lname -AccountPassword (ConvertTo-SecureString $Upassword -AsPlainText -Force ) -Path $office -Department $dept -Enabled $true
    
    echo "Utilisateur ajouté : $name" } catch{
    
    echo "utilisateur non ajouté : $name" }  }
    

    Following the logic did this script to sort them into my "departement" named "Reglements locaux" and it wasn't very successful

    foreach ($user in $users){
    
    $name = $user.firstName + " " + $user.lastName
    
    $fname = $user.firstName
    
    $lname = $user.lastName
    
    $login = $user.firstName + "." + $user.lastName
    
    $Uoffice = $user.office
    
    $Upassword = $user.password
    
    $dept = $user.department
    
    switch($user.departement){
    
    "Reglements locaux" {$dept = "OU=Reglements locaux,OU=Comptabilite,OU=Exercise1,DC=college,DC=local"}
    
    default {$dept = $null}
    
    }  try {  Add-ADUser -Name $name -SamAccountName $login -UserPrincipalName $login -DisplayName $name -GivenName $fname -Surname $lname -AccountPassword (ConvertTo-SecureString $Upassword -AsPlainText -Force ) -Path $dept -Department $dept -Enabled $true
    
    echo "Utilisateur ajouté : $name"  } catch{
    
    echo "utilisateur non ajouté : $name"}  }
    
    

    I also tried to put "Move-ADUser" instead of "Add-ADUser" but it still doesnt work. I give me my list of users with the mention "not added"

     

    No error message neither! Can somenone help?

     

  • #181419

    Participant
    Topics: 6
    Replies: 97
    Points: 239
    Helping Hand
    Rank: Participant

    Looks like the scripts are basically identical other than using $dept instead of $office for the -Path parameter. And I don't see a cmdlet called Add-ADUser. Where is that coming from? Am I just looking at a different version of the ActiveDirectory module?

    Are the users you are trying to put in the "OU=Reglements locaux,OU=Comptabilite,OU=Exercise1,DC=college,DC=local" OU new users that you're creating in this script or are they existing users you just need to move?

    Also, you're probably getting an error thrown by that catch block, but you aren't writing it to the console. Try adding "Write-Error $_.Message" to the catch block after your "utilisateur non ajouté : $name" line, (or just "Write-Error $_" for the full exception).

You must be logged in to reply to this topic.