简体   繁体   中英

Importing Users into Active Directory

I'm trying to create a script that will import a bunch of users into AD using the following CSV file. I need the users to be created into a custom OU based on the department and if the OU does not exist, I need the script to create the OU then place the user in corresponding OU.

Content of CSV:

firstname,lastname,dept
Albina,Crinklaw,FRESHMAN
Alona,Harbin,FRESHMAN
Athena,Canada,FRESHMAN
Ronnie,Castagna,FRESHMAN
Teofila,Lambros,JUNIORS
Lashonda,Meals,JUNIORS
Serena,Stricker,JUNIORS
Jeraldine,Lonergan,JUNIORS
Angelica,Cordle,JUNIORS
Risa,Shutt,JUNIORS
Tamara,Rough,JUNIORS
Jefferey,Michel,JUNIORS
Candie,Elderkin,JUNIORS
Nguyet,Mcdonagh,JUNIORS
Delbert,Hetzler,JUNIORS
Tammi,Dietrich,JUNIORS
Nam,Gendreau,JUNIORS
Sherice,Shotwell,JUNIORS
Un,Simms,JUNIORS
Ettie,Stitt,SENIORS
Peg,Huber,SENIORS
Violet,Valerio,SENIORS
Remona,Stonerock,SENIORS
Ian,Grizzle,SENIORS
Jeremiah,Chock,SENIORS
Kenya,Carasco,SENIORS
Olinda,Stills,SENIORS
Kristie,Kasten,SENIORS
Jude,Roesner,SENIORS
Bret,Erwin,SENIORS
Emily,Mckay,SENIORS
Cecila,Scheel,SENIORS
Wiley,Dobbin,SENIORS
Terrilyn,Westrick,SENIORS
Thao,Kissell,SOPHOMORES
Nichelle,Edelman,SOPHOMORES
Rubin,Brocato,SOPHOMORES
Mel,Perlmutter,SOPHOMORES
Hellen,Mayr,SOPHOMORES
Omega,Maskell,SOPHOMORES
Chelsea,Orcutt,SOPHOMORES
Janyce,Madigan,SOPHOMORES
Krysta,Delagarza,SOPHOMORES
Alejandro,Kish,SOPHOMORES
Pura,Morant,SOPHOMORES

Script:

$UserList = Import-Csv -Path 'C:\users\administrator\Desktop\its3410Users.csv' 

foreach ($User in $UserList) {

     $Attributes = @{

        Enabled = $true
        ChangePasswordAtLogon = $true
        Path = @{Name="Path";Expression={Get-ADOrganizationalUnit -Filter "name -eq '$($_.department)'"}}

        Name = "$($User.Firstname) $($User.lastname)"
        UserPrincipalName = "$($User.firstname).$($User.lastname)@its3410.net"
        SamAccountName = "$($User.firstname).$($User.lastname)"

        GivenName = $User.firstname
        Surname = $User.lastname

   
        Department = $User.dept
        AccountPassword = "Ch@nge123Me!" | ConvertTo-SecureString -AsPlainText -Force}

     }

    New-ADUser @Attributes

Your CSV shows the third column is called dept , but your code uses department Also, since you are looping with foreach ($User in $UserList) {..} and not $UserList | ForEach-Object {..} $UserList | ForEach-Object {..} , inside the loop there is no $_ automatic variable and you need to use $User.dept .

Your code revised:

$UserList = Import-Csv -Path 'C:\users\administrator\Desktop\its3410Users.csv'

foreach ($User in $UserList) {
    $OU = Get-ADOrganizationalUnit -Filter "name -eq '$($User.dept)'" -ErrorAction SilentlyContinue
    if (!$OU) {
        # create the new OU (set the Path to the DistinguishedName of the root path for users/departments)
        $OU = New-ADOrganizationalUnit -Name $User.dept -Path "DC=ITS3410,DC=NET" -PassThru
    }  
    $Attributes = @{
        Enabled               = $true
        ChangePasswordAtLogon = $true
        Path                  = $OU.DistinguishedName
        Name                  = '{0} {1}' -f $User.firstname, $User.lastname
        UserPrincipalName     = '{0}.{1}@its3410.net' -f $User.firstname, $User.lastname
        SamAccountName        = '{0}.{1}' -f $User.firstname, $User.lastname
        GivenName             = $User.firstname
        Surname               = $User.lastname
        Department            = $User.dept
        AccountPassword       = "Ch@nge123Me!" | ConvertTo-SecureString -AsPlainText -Force
    }

    New-ADUser @Attributes
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM