將資料匯入我的目錄
有數種方式可讓您使用 PowerShell 來將資料匯入您的目錄,但最常用的方法是使用 CSV 檔案。 這個檔案可以手動建立 (例如使用 Excel),也可以從現有的資料來源 (例如 SQL Database 或 HR 應用程式) 匯出。 以下是將資料匯入 Azure Active Directory 的一些常見案例:
大量建立新的使用者
如果您想要在 Azure Active Directory 中大量建立使用者,並且有一個包含您想要建立之新使用者相關資訊的 CSV 檔案,則以下是一些考量:
- 您需要建立或實作使用者名稱、顯示名稱和別名的命名慣例。 例如,使用者名稱的建構方式是從使用者的姓氏開始,後面接著句點,然後接著名字。 因此,contoso.com 中的 "John Smith" 會具有 "Smith.John@contoso.com" 的使用者名稱。 您將需要針對顯示名稱和別名實作類似的規則。以顯示名稱為例,可以考慮將部門名稱加入,例如 "John Smith (Accounting)"。
- 您需要針對新建立的使用者,實作初始密碼慣例。而且需要找出一種安全的方式,讓新使用者能透過此方式接收密碼。 針對此情況,通常使用的方法是產生隨機密碼,然後透過電子郵件寄送給新使用者或其經理。
假設您已完成這些需求,讓我們看看用來建立新使用者的 CSV 檔案需要包含哪些內容。 以下是本文中所使用的 CSV 檔案範例:
Firstname,MiddleInitials,Lastname,Department,JobTitle
Peter,S,Fischer,Development,Manager
James,T,McGuire,Research,Assistant
Karen,R,Cristo,Sales,Director
若要使用 PowerShell 大量建立這些使用者,需要採取幾個步驟:
- 使用您的目錄來設定 PowerShell 工作階段:我們將使用
Connect-AzureAD
Cmdlet 來執行此動作。 - 在 PowerShell 工作階段中,使用
Import-CSV
Cmdlet 來從 CSV 檔案匯入資料。 - 針對檔案中的每一行,建立必要的參數來建立新使用者
- 執行此 Cmdlet,以使用
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 啟用帳戶。