Powershell Bulk User Import in Server 2012

简介:

Modify CSV File

Now that you have a CSV file with all your users and extra information, we need to format it so that certain fields can be imported. We will also need to add a few fields. For a list of all fields that Powershell can import check out Microsoft’s TechNet Article. In my file I used the following columns:

  • Username

  • GivenName (or first name)

  • MiddleInitial

  • Surname (last name)

  • Display Name (Full Name)

  • UserPrincipalName

  • StreetAddress

  • City

  • State

  • ZipCode

  • Password

  • TelephoneNumber

  • Occupation

  • Path

  • Office

Some of these items I want to go into more detail about because they are tricky to make.

  • Username, for my domain, will consist of First initial + Last name. So I created a new column in my CSV file and used this function to create the username: =CONCATENATE(LEFT(B2,1), D2) Where as B is the GivenName column and D is the Surname column. Example: Danny Eckes becomes DEckes.

  • UserPrincipalName, is the logon name and domain on the Account tab of an Active Directory user properties. Since this one column fills two items you need to make sure this is done correctly. The function I used for this column is =A2&”@eckescorp.local” Where as A is the Username column and the text @eckescorp.local is the name of my fake domain. Active directory will use the @ to separate the two items. If you misspell something here you can run into trouble. Example: DEckes becomes DEckes@eckescorp.local.

  • DisplayName, for my domain, is First name and Last name. You can also add Middle Initial if you wanted. However when looking at the Name field of Active Directory, First and Last name look fine for me. The function I used for DisplayName is: =B2&” “&D2 Where B is the GivenName column and D is the Surname column. Note that the quotes have a space in the middle of them. This separates the first and last name. Example: Danny Eckes becomes one cell of Danny Eckes.

  • Password, this column is the value I will use for the password. Since I am doing this for testing and I want all users to have the same password I just set it to Password123.

  • Path is the Active Directory Path to the Organizational Unit the user object will be created. It helps to read this from right to left. OU=Los Angeles,OU=ecorp – Users,DC=eckescorp,DC=local when read means to create the new user on the Eckescorp.local Domain Controller in side the Los Angeles Organizational Unit that is a child of the ecorp – Users Organizational Unit. You will need to adjust this to match your DC tree structure.

You can take a look at my completed CSV file here.

Create Powershell Script

Now that we have our CSV file we can create a script that will import specific AD values from the columns in the CSV file. Open up Notepad and save the file as a .ps1 file. Name it whatever is easiest for you. Your script will consist of 2 lines:

  • Line 1: Import the CSV file and for each row of the CSV do the next line. Make sure you have the CSV filename and path correct. In my case the CSV and the script are in the same folder.

  • Line 2: Match the AD fields to the columns of the CSV. -FIELDNAME is the AD object and $_.COLUMNNAME is the item from the CSV. Example -Name $_.DisplayName will place the value of the cell in the DisplayName column as the Name in Active Directory.

    • -AccountPassword is a tricky one to set. I would suggest copying mine. It pulls the cell from the Password column and converts it to a secure string which is required for passwords.

    • -Enabled $True sets the account as enabled.

    • -PasswordNeverExpires $True will set the option for password on this account to NOT expire.

For simplicity you can use the following script:

PowerShell

Import-Csv .\userImport.csv | foreach-object { New-ADUser -Name $_.DisplayName -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.Username -GivenName $_.GivenName -DisplayName $_.DisplayName -Initials $_.MiddleInitial -SurName $_.Surname -Description $_.Description -Department $_.Department -StreetAddress $_.StreetAddress -City $_.City -State $_.State -PostalCode $_.ZipCode -HomePhone $_.TelephoneNumber -Title $_.Occupation -Office $_.Office -Path $_.Path -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -force) -Enabled $True -PasswordNeverExpires $True -PassThru }

1

2

Import-Csv .\userImport.csv | foreach-object {

New-ADUser -Name $_.DisplayName -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.Username -GivenName $_.GivenName -DisplayName $_.DisplayName -Initials $_.MiddleInitial -SurName $_.Surname-Description $_.Description -Department $_.Department -StreetAddress $_.StreetAddress -City $_.City -State$_.State -PostalCode $_.ZipCode -HomePhone $_.TelephoneNumber -Title $_.Occupation -Office $_.Office -Path $_.Path -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -force) -Enabled $True -PasswordNeverExpires $True -PassThru }


Run PowerShell Script

If you haven’t yet, you will need to make sure you have enabled Powershell scripts on your server. Follow my article here to do that. Put your CSV files and Powershell script on your DC, or server that you have Active Directory installed on. I put them in C:\Temp but if you’re going to run a lot of scripts you might want to create a C:\Scripts folder.

Launch Powershell as an Administrator. Then at the prompt change directory “CD” to get to the folder that holds your scripts. Once in that folder, type:

PowerShell

.\ScriptName.ps1

1

.\ScriptName.ps1

Screen Shot 2013-08-24 at 5.05.30 PM

The Script will run and in seconds import all your users. Head over to Active Directory Users and Computers to confirm all the users are in the right locations and have the right attributes.

Screen Shot 2013-08-24 at 3.14.52 PM

I ran into several snags while importing these users. I first ran the script with just 2 users in my CSV file until I got all the kinks ironed out. I then imported the larger CSV file. I hope this explanation helps you get a basic understanding of Powershell New-ADUser Bulk Importing. Feel free to leave comments below.

- See more at: http://www.dannyeckes.com/powershell-bulk-user-import-in-server-2012/#sthash.igYnT1oN.dpuf

本文转自学海无涯博客51CTO博客,原文链接http://blog.51cto.com/549687/1868503如需转载请自行联系原作者


520feng2007

相关文章
|
XML Java 关系型数据库
IDEA 报错: java.sql.SQLException: Access denied for user ‘root ‘@‘localhost‘ (using password: YES)
IDEA 报错: java.sql.SQLException: Access denied for user ‘root ‘@‘localhost‘ (using password: YES)
875 0
解决java.sql.SQLException: Access denied for user ‘Lenovo‘@‘localhost‘ (using password: YES)问题~
解决java.sql.SQLException: Access denied for user ‘Lenovo‘@‘localhost‘ (using password: YES)问题~
131 0
|
XML 数据格式
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source‘ to ‘org.eclips
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source‘ to ‘org.eclips
112 0
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source‘ to ‘org.eclips
|
应用服务中间件 Android开发
Could not publish server configuration for Tomcat v6.0 Server at localhost. Multiple Contexts.
Could not publish server configuration for Tomcat v6.0 Server at localhost. Multiple Contexts.
141 0
Could not publish server configuration for Tomcat v6.0 Server at localhost. Multiple Contexts.
|
XML 应用服务中间件 Android开发
解决方案:Tomcat警告 [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source‘
解决方案:Tomcat警告 [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source‘
解决方案:Tomcat警告 [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source‘
|
SQL Windows
SQL Server安装提示【需要microsoft.NET Framework 3.5 Service Pack 1】
SQL Server安装提示【需要microsoft.NET Framework 3.5 Service Pack 1】
836 0
SQL Server安装提示【需要microsoft.NET Framework 3.5 Service Pack 1】