在 entra 識別碼中大量新增群組成員Microsoft
您可以使用逗號分隔值 (CSV) 檔案,將多個成員新增至群組,以在入口網站中大容量導入群組成員,以取得Microsoft Entra ID。
了解 CSV 範本
下載並填入大量上傳 CSV 範本,以大量新增Microsoft Entra 群組成員。 您的 CSV 範本看起來可能像下列範例:
CSV 範本結構
下載的 CSV 樣本中的數據列如下:
- 版本號碼:包含版本號碼的第一個數據列必須包含在上傳 CSV 中。
- 資料行標題:資料行標題的格式為<項目名稱> [屬性名稱]<必要或空白>。 例如
Member object ID or user principal name [memberObjectIdOrUpn] Required
。 部分較舊版本的範本可能有些微變化。 針對群組成員資格變更,您可以選擇成員物件識別碼或用戶主體名稱。 - 範例數據列:範本包含每個數據行可接受的值範例數據列。 您必須移除範本列並取代為您本身的項目。
更多指導
- 上傳範本的前兩個數據列不得移除或修改,或無法處理上傳。
- 首先會列出必要的資料行。
- 我們不建議在範本中新增新資料行。 您新增的任何其他數據行會被忽略,且不會處理。
- 我們建議您盡可能下載最新版本的 CSV 範本。
- 新增至少兩個使用者的UPN或物件識別碼,以成功上傳檔案。
大容量導入群組成員
提示
根據您開始使用的入口網站,本文中的步驟可能略有不同。
至少以群組系統管理員的身分登入 Microsoft Entra 系統管理中心。
選取 [Microsoft Entra ID]。
注意
群組擁有者也可以大容量導入他們所擁有的群組成員。
選取 [群組>所有群組]。
開啟您要新增成員的群組,然後選取 [ 成員]。
在 [ 成員] 頁面上,選取 [ 大量作業 ],然後選擇 [ 匯入成員]。
在 [ 大容量導入群組成員] 頁面上,選取 [ 下載 ] 以取得具有必要群組成員屬性的 CSV 檔案範本。
開啟 CSV 檔案,併為您要匯入群組的每個群組成員新增一行。 必要值為 Member 物件識別碼 或 用戶主體名稱。 然後儲存檔案。
在 [大容量導入群組成員] 頁面的 [上傳 csv 檔案] 底下,流覽至檔案。 當您選取檔案時,CSV 檔案的驗證就會開始。
驗證檔案內容時,大容量導入頁面會顯示 已成功上傳的檔案。 如果發生錯誤,您必須修正這些錯誤,才能提交作業。
當您的檔案通過驗證時,請選取 [ 提交 ] 以啟動將群組成員匯入至群組的大量作業。
匯入作業完成時,通知會指出大量作業成功。
如果您遇到錯誤,您可以在 [大量作業結果] 頁面上下載並檢視結果檔案。 此檔案包含每個錯誤的原因。 檔案提交必須符合提供的範本,並包含確切的數據行名稱。 如需大量作業限制的詳細資訊,請參閱 大容量導入服務限制。
檢查匯入狀態
您可以在 [大量作業結果] 頁面上看到所有暫止大量要求的狀態。
如需有關大量作業內每行項目的詳細資料,請選取 [# 成功]、[#失敗] 或 [總計要求] 資料行底下的值。 如果發生失敗,會列出失敗的原因。
大容量導入服務限制
您應該知道,每個大量作業活動最多可以執行一小時。
Microsoft Entra 管理入口網站中的大量作業可能會在非常大型的租使用者上逾時並失敗。 這項限制是已知問題,因為調整限制。 Microsoft工程小組正致力於最終解決這項限制的新服務。
注意
執行大量作業,例如匯入或建立時,如果大量作業未在一小時內完成,可能會遇到問題。 若要解決此問題,建議您分割每個批次處理的記錄數目。 例如,在啟動匯出之前,您可以篩選群組類型或使用者名稱來限制結果集,以減少結果的大小。 藉由精簡篩選,基本上您會限制大量作業傳回的數據。
此問題的另一個因應措施是使用PowerShell直接Microsoft Graph API呼叫。 針對大量下載使用者和群組失敗,我們建議使用 PowerShell Cmdlet GET-MgGroup -All
和 GET-MgUser -All
。
下列 PowerShell 程式代碼範例適用於與下列相關的大量作業:
使用者
大量下載所有使用者
# Import the Microsoft Graph module
Import-Module Microsoft.Graph
# Authenticate to Microsoft Graph (you may need to provide your credentials)
Connect-MgGraph -Scopes "User.Read.All"
# Get all users using Get-MgUser
$users = Get-MgUser -All -ConsistencyLevel eventual -Property Id, DisplayName, UserPrincipalName,UserType,OnPremisesSyncEnabled,CompanyName,CreationType
# Specify the output CSV file path
$outputCsvPath = "C:\\Users\\YourUsername\\Documents\\Users.csv"
# Create a custom object to store user data
$userData = @()
# Loop through each user and collect relevant data
foreach ($user in $users) {
$userObject = [PSCustomObject]@{
Id = $user.Id
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
UserType = $user.UserType
OnPremisesSyncEnabled = $user.OnPremisesSyncEnabled
CompanyName = $user.CompanyName
CreationType = $user.CreationType
}
$userData += $userObject
}
# Export user data to a CSV file
$userData | Export-Csv -Path $outputCsvPath -NoTypeInformation
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "User data exported to $outputCsvPath"
大量建立使用者
# Import the Microsoft Graph module
Import-Module Microsoft.Graph
# Authenticate to Microsoft Graph (you may need to provide your credentials)
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Specify the path to the CSV file containing user data
$csvFilePath = "C:\\Path\\To\\Your\\Users.csv"
# Read the CSV file (adjust the column names as needed)
$usersData = Import-Csv -Path $csvFilePath
# Loop through each row in the CSV and create users \
foreach ($userRow in $usersData) {
$userParams = @{
DisplayName = $userRow.'Name [displayName] Required'
UserPrincipalName = $userRow.'User name [userPrincipalName] Required'
PasswordProfile = @{
Password = $userRow.'Initial password [passwordProfile] Required'
}
AccountEnabled = $true
MailNickName = $userRow.mailNickName
}
try {
New-MgUser @userParams
Write-Host "User $($userRow.UserPrincipalName) created successfully."
} catch {
Write-Host "Error creating user $($userRow.UserPrincipalName): $($_.Exception.Message)"
}
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Bulk user creation completed."
注意
請確定您的 CSV 檔案包含必要的資料列(例如、DisplayName
UserPrincipalName
、 等等)。 此外,請調整文本,以符合 CSV 檔案中的實際數據行名稱。
大量刪除使用者
# Import the Microsoft Graph module
Import-Module Microsoft.Graph
# Authenticate to Microsoft Graph (you may need to provide your credentials)
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Specify the path to the CSV file containing user data
$csvFilePath = "C:\\Path\\To\\Your\\Users.csv"
# Read the CSV file (adjust the column names as needed)
$usersData = Import-Csv -Path $csvFilePath
# Loop through each row in the CSV and delete users
foreach ($userRow in $usersData) {
try {
Remove-MgUser -UserId $userRow.UserPrincipalName -Confirm:$false
Write-Host "User $($userRow.UserPrincipalName) deleted successfully."
} catch {
Write-Host "Error deleting user $($userRow.UserPrincipalName): $($_.Exception.Message)"
}
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Bulk user deletion completed."
注意
請確定您的 CSV 檔案包含必要的資料列(例如 )。 UserPrincipalName
此外,請調整文本,以符合 CSV 檔案中的實際數據行名稱。
群組
大量下載所有群組
Import-Module Microsoft.Graph.Groups
# Authenticate to Microsoft Graph (you may need to provide your credentials)
Connect-MgGraph -Scopes "Group.Read.All"
# Get the group members
$groups = Get-MgGroup -All | Select displayName, Id, groupTypes,mail
# Create a custom object to store group data
$groupData = @()
# Loop through each group and collect relevant data
foreach ($group in $groups) {
if ($group.groupTypes -contains "Unified"){$groupType = "Microsoft 365"}
else {$groupType = "Security"}
if ($group.groupTypes -contains "DynamicMembership"){$membershipType = "Dynamic"}
else {$membershipType = "Assigned"}
$groupObject = [PSCustomObject]@{
Id = $group.Id
DisplayName = $group.displayName
Mail = $group.mail
GroupType = $groupType
MemebershipType = $membershipType
}
$groupData += $groupObject
}
# Specify the output CSV file path
$outputCsvPath = "C:\\Users\\cewu\\Documents\\Groups.csv"
$groupData| Export-Csv -Path $outputCsvPath -NoTypeInformation
Write-Host "Group members exported to $outputCsvPath"
大量下載群組成員
Import-Module Microsoft.Graph.Groups
# Authenticate to Microsoft Graph (you may need to provide your credentials)
Connect-MgGraph -Scopes "Group.Read.All,GroupMember.Read.All"
# Set the group ID of the group whose members you want to download
$groupId = "your_group_id"
# Get the group members
$members = Get-MgGroupMember -GroupId $groupId -All | select * -ExpandProperty additionalProperties | Select-Object @(
'id'
@{ Name = 'userPrincipalName'
Expression = { $_.AdditionalProperties["userPrincipalName"] }
}
@{ Name = 'displayName'
Expression = { $_.AdditionalProperties["displayName"] }
}
)
# Specify the output CSV file path
$outputCsvPath = "C:\\Users\\YourUserName\\Documents\\GroupMembers.csv"
$members| Export-Csv -Path $outputCsvPath -NoTypeInformation
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Group members exported to $outputCsvPath"
大量新增成員
Import-Module Microsoft.Graph.Groups
# Authenticate to Microsoft Graph (you may need to provide your credentials)
Connect-MgGraph -Scopes "GroupMember.ReadWrite.All"
# Import the CSV file
$members = Import-Csv -Path "C:\path\to\your\file.csv"
# Define the Group ID
$groupId = "your-group-id"
# Iterate over each member and add them to the group
foreach ($member in $members) {
try{
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $member.memberObjectId
Write-Host "Added $($member.memberObjectId) to the group."
}
Catch{
Write-Host "Error adding member $($member.memberObjectId):$($_.Exception.Message)"
}
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
大量移除成員
Import-Module Microsoft.Graph.Groups
# Authenticate to Microsoft Graph (you may need to provide your credentials)
Connect-MgGraph -Scopes "GroupMember.ReadWrite.All"
# Import the CSV file
$members = Import-Csv -Path "C:\path\to\your\file.csv"
# Define the Group ID
$groupId = "your-group-id"
# Iterate over each member and add them to the group
foreach ($member in $members) {
try{
Remove-MgGroupMemberByRef -GroupId $groupId -DirectoryObjectId $member.memberObjectId \
Write-Host "Removed $($member.memberObjectId) from the group."
}
Catch{
Write-Host "Error removing member $($member.memberObjectId):$($_.Exception.Message)"
}
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
裝置
大量下載所有裝置
Import-Module Microsoft.Graph
# Authenticate to Microsoft Graph (you may need to provide your credentials)
Connect-MgGraph -Scopes "Device.Read.All"
# Get all devices
$devices = Get-MgDevice -All |select displayName,deviceId,operatingSystem,operatingSystemVersion,isManaged,isCompliant,mdmAppId,registeredOwners,TrustType
# Specify the output CSV file path
$outputCsvPath = "C:\\Users\\YourUserName\\Documents\\Devices.csv"
$devices| Export-Csv -Path $outputCsvPath -NoTypeInformation
Write-Host "Devices exported to $outputCsvPath"