Jump to content

Active Directory Import with CSV to different OU's


pops
 Share

Recommended Posts

I'm working on a script to import users into Active Directory and it is working with no issue, all users are dropped into the respective OU that was written in the original script. I modified the script to add an array with job titles and added an if statement to place the new users into different OU's based on their job titles but receive an error that it cannot validate the argument on parameter 'Path'. I an not sure if the code it correct or how to go about referencing PATH. I don't really know the best method for this and i may be approaching it completely wrong but below is my current script. Its pretty basic to get the users into AD but this addition is making me scratch my head. Any help would be much appreciated.

Import-Module ActiveDirectory

 #Enter a path to your import CSV file
     $ADUser = Import-csv C:\ADS\NewHiresRev1.csv

    $OU = "JOB TITLE 1","JOB Title 2","JOB Title 3"

  
       $Domain              = "mydomain.com"
       $Username             = $user.FirstName + "." + $user.LastName
       $Password            = "Welcome"+$user.employeenumber+"!"
       $secpasswd           = ConvertTo-SecureString $Password -AsPlainText -Force
       $GivenName           = $user.firstname
       $Surname             = $user.lastname
       $Name                  = $user.firstname + " " + $user.lastname
       $DisplayName          = $user.firstname + " " + $user.lastname
       $Description           = $user.job
       $UserPrincipalName      = $user.firstname + "." + $user.lastname + "@" + $Domain
       $Enable                 = $true
       $ChangePasswordAtLogon    = $false
       $Organization          = "ScriptedUsers"
       $Department           = "Operations"
       $Office               = $user.job
       $Company              = "Company Inc."
       $Manager               = "Manager Name"
       $StreetAddress         = ""
       $City                 = ""
       $State                 = ""
       $PostalCode           = ""
       $Country              = "US"

      
#Check if the user account already exists in AD
       if (Get-ADUser -F {SamAccountName -eq $Username})
       {
               #If user does exist, output a warning message
               Write-Warning "A user account $SamAccountName has already exist in Active Directory."
       }
       else
       {



 function identifyOU() {
         foreach ($User in $ADUser){
        if($user.job -contains $OU) {
            $UsersOU = "OU=MDaemon Users,DC=ad,DC=cg-idd,DC=com"
            }
            else {"OU=Company Users Users,DC=ad,DC=cg-idd,DC=com"}
            }
        }
              #If a user does not exist then create a new user account

        #Account will be created in the OU listed in the $OU variable in the CSV file; don’t forget to change the domain name in the"-UserPrincipalName" variable
         New-ADUser `
            -Path $UsersOU `
            -SamAccountName $Username `
            -AccountPassword $secpasswd `
            -GivenName $GivenName `
            -Surname $Surname `
            -Name $Name  `
            -DisplayName $DisplayName `
            -Description $Description `
            -UserPrincipalName $UserPrincipalName `
            -Enabled $True `
            -ChangePasswordAtLogon $False `
            -Organization $Organization `
            -Department $Department `
            -Office $Office `
            -Title $Title `
            -Company $Company `
            -StreetAddress $StreetAddress `
            -City $City `
            -State $State `
            -PostalCode $PostalCode `
        -Country $Country `

       }

 

Edited by JLogan3o13
Link to comment
Share on other sites

  • Moderators

So, a couple of things I see:

  • First and foremost, please provide an example of the layout of NewHiresRev1.csv.
  • Secondly, you're referencing vars like $user.FirstName and $user.LastName long before you create the $User variable. It looks as though this is not all of your code (as evidenced by the identifyOU function being at the bottom and inside an if/else statement. If this is all of your code, there is no wonder why it is failing.
  • Lastly, when posting on the forum, you might want to remove real references to your company. It looks like they do good work, but it is usually a good idea to sanitize code before posting.
Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...