你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

将数据导入目录

可通过多种方法使用 PowerShell 将数据导入目录,但最常用的方法是使用 CSV 文件。 可手动创建(例如使用 Excel)此文件,也可从现有数据源(例如 SQL 数据库或 HR 应用程序)中导入此文件。 下面是将数据导入 Azure Active Directory 的几种常见方案:

批量创建新用户

如果要在 Azure Active Directory 中批量创建用户,并且有一个包含要创建的新用户相关信息的 csv 文件,请注意以下事项:

  1. 需要建立或实现用于用户名、显示名称和别名的命名约定。 例如:用户名组成结构为用户的姓氏后跟句点、名字。 因此,contoso.com 中“John Smith”的用户名为“Smith.John@contoso.com”。 你需要为显示名称(也许你想要将部门名称添加到显示名称,如“John Smith (会计) ”)和别名实现类似的规则。
  2. 需要为新创建用户的初始密码实施一个约定,还需找到让新用户以安全方式接收密码的方法。 常用方法是生成随机密码,然后通过电子邮件将其发送给新用户或管理员。

假设已建立这些命名约定,接下来看看用于创建新用户的 CSV 文件需包含哪些内容。 下面是本文中使用的 CSV 文件示例:

Firstname,MiddleInitials,Lastname,Department,JobTitle
Peter,S,Fischer,Development,Manager
James,T,McGuire,Research,Assistant
Karen,R,Cristo,Sales,Director

若要使用 PowerShell 批量创建这些用户,需执行以下几个步骤:

  1. 设置与目录的 PowerShell 会话 - 为此,使用 Connect-AzureAD cmdlet。
  2. 使用 Import-CSV cmdlet 从 PowerShell 会话的 CSV 文件中导入数据。
  3. 为文件中的每行创建所需参数,用于创建新用户
  4. 执行 New-AzureADUser cmdlet 以创建新用户

如果在生产环境中使用此脚本,可能还需添加某些其他部分,例如:

  • 错误处理 - 无法创建用户时要执行的操作
  • 执行报告 - 脚本完成后,提供已执行操作的概述,例如创建的用户数等

现跳过这些部分,重点介绍核心功能。

以下脚本可用于导入所示 CSV 文件并创建新用户。 要使此脚本在环境中正常工作,需将第一部分的值更新为适合自身环境的正确值。

请注意:新用户的密码必须符合为目录设置的密码复杂性规则。 请注意:此处指定的管理员帐户必须是要指定的目录中的管理员

###
### These are vairable you need to update to reflect your environment
###

$Admin = "Admin@contoso.com"
$AdminPassword = "my password"
$Directory = "contoso.com"
$NewUserPassword = "newuserspasswords"
$CsvFilePath = "C:\work\users.csv"


###
### Create a PowerShell connection to my directory. If you do not want to specify the password in the script, you can simply replace this with "Connect-AzureAD", which will prompt for a username and password.
###

$SecPass = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($Admin, $SecPass)
Connect-AzureAD -Credential $cred

###
### Create a new Password Profile for the new users. We'll be using the same password for all new users in this example
###

$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = $NewUserPassword

###
### Import the csv file. You will need to specify the path and file name of the CSV file in this cmdlet
###

$NewUsers = import-csv -Path $CsvFilePath

###
### Loop through all new users in the file. We'll use the ForEach cmdlet for this.
###

Foreach ($NewUser in $NewUsers) { 

###
### Construct the UserPrincipalName, the MailNickName and the DisplayName from the input data in the file 
###

    $UPN = $NewUser.Firstname + "." + $NewUser.LastName + "@" + $Directory
    $DisplayName = $NewUser.Firstname + " " + $NewUser.Lastname + " (" + $NewUser.Department + ")"
    $MailNickName = $NewUser.Firstname + "." + $NewUser.LastName

###
### Now that we have all the necessary data for to create the new user, we can execute the New-AzureADUser cmdlet  
###

    New-AzureADUser -UserPrincipalName $UPN -AccountEnabled $true -DisplayName $DisplayName -GivenName $NewUser.FirstName -MailNickName $MailNickName -Surname $NewUser.LastName -Department $Newuser.Department -JobTitle $NewUser.JobTitle -PasswordProfile $PasswordProfile

###
### End the Foreach loop
###
    }

注意:在成功执行此脚本后,新用户将在 Azure Active Directory 中创建。 由于我们指定了 -AccountEnabled = $True,因此新用户可以立即使用其密码登录到目录。 如果不希望用户帐户在运行脚本后直接处于活动状态,则可以指定 -AccountEnabled = $False,然后使用 Set-AzureADUser cmdlet 启用帐户。