共用方式為


Microsoft Entra ID 中的大量還原已刪除使用者

Microsoft Entra 識別碼,Microsoft Entra 的一部分,支援大量使用者還原作業,並支援下載使用者、群組和群組成員的清單。

了解 CSV 範本

下載並填入 CSV 範本,以協助您成功還原大量Microsoft Entra 使用者。 您下載的 CSV 範本可能看起來會是此範例:

用於上傳和註標的電子錶格螢幕快照,其中說明每個數據列和數據行的用途和值。

CSV 範本結構

下載的 CSV 範本資料列為以下:

  • 版本號碼:上傳 CSV 的第一列必須包含版本號碼。
  • 資料行標題:資料行標題的格式為<項目名稱> [屬性名稱]<必要或空白>。 例如: Object ID [objectId] Required 。 部分較舊版本的範本可能有些微變化。
  • 範例資料列:我們已在範本的各資料行中加入一列可接受的範例值。 您必須移除範本列並取代為您本身的項目。

其他指導方針

  • 上傳範本的前面兩列不得移除或修改,否則無法進行上傳。
  • 首先會列出必要的資料行。
  • 我們不建議在範本中新增新資料行。 您新增的任何其他資料行已略過或尚未處理。
  • 我們建議您盡可能下載最新版本的 CSV 範本。

若要大量還原使用者

提示

根據您開始使用的入口網站,本文中的步驟可能略有不同。

  1. 以至少是使用者管理員的身分登入 Microsoft Entra 系統管理中心

  2. 選取 [Microsoft Entra ID]。

  3. 選取 [>>所有使用者已刪除]。

  4. 在 [ 已刪除的使用者 ] 頁面上,選取 [ 大量還原 ] 以上傳要還原之使用者屬性的有效 CSV 檔案。

    在 [已刪除的使用者] 頁面上選取大量還原命令的螢幕快照。

  5. 開啟 CSV 範本,並為您要還原的每個使用者新增一行。 唯一必要的值為 ObjectID。 然後儲存檔案。

    選取您要新增之使用者的本機 CSV 檔案螢幕快照

  6. 在 [大量還原] 頁面上的 [上傳 csv 檔案] 底下,流覽至檔案。 當您選取檔案並按兩下 [ 提交] 時,CSV 檔案的驗證就會開始。

  7. 驗證檔案內容時,您會看到 已成功上傳檔案。 如果發生錯誤,您必須修正這些錯誤,才能提交作業。

  8. 當您的檔案通過驗證時,請選取 [ 提交 ] 以啟動還原使用者的大量作業。

  9. 當還原作業完成時,您會看到大量作業成功通知。

如果您遇到錯誤,您可以在 [大量作業結果] 頁面上下載並檢視結果檔案。 此檔案包含每個錯誤的原因。 檔案提交必須符合提供的範本,並包含確切的數據行名稱。 如需大量作業限制的詳細資訊,請參閱 大量還原服務限制

檢查狀態

您可以在 [大量作業結果] 頁面中看到所有暫止大量要求的狀態。

在 [大量作業結果] 頁面中檢查狀態的螢幕快照。

接下來,您可以透過 Microsoft Entra ID 或 PowerShell,檢查您還原的使用者是否存在於 Microsoft Entra 組織中。

在 Azure 入口網站 中檢視已還原的使用者

  1. 以至少是使用者管理員的身分登入 Microsoft Entra 系統管理中心
  2. 選取 [Microsoft Entra ID]。
  3. 選取 [管理] 下的 [所有使用者],選取 [使用者]。
  4. 在 [顯示] 底下,選取 [所有使用者],並確認您已還原的使用者已列出。

使用 PowerShell 檢視使用者

執行以下命令:

Get-MgUser -Filter "UserType eq 'Member'"

您應該會看到您所還原的使用者已列出。

注意

自 2024 年 3 月 30 日起,Azure AD 和 MSOnline PowerShell 模組已被淘汰。 若要深入了解,請閱讀淘汰更新。 在此日期之後,對這些模組的支援僅限於對 Microsoft Graph PowerShell SDK 的移轉協助和安全性修正。 淘汰的模組將繼續運作至 2025 年 3 月 30 日。

我們建議移轉至 Microsoft Graph PowerShell 以與 Microsoft Entra ID (以前稱為 Azure AD) 互動。 如需了解常見的移轉問題,請參閱移轉常見問題注意:MSOnline 1.0.x 版可能會在 2024 年 6 月 30 日之後發生中斷。

大量還原服務限制

您應該知道,每個大量作業活動最多可以執行一小時。

Microsoft Entra 管理入口網站中的大量作業可能會在非常大型的租使用者上逾時並失敗。 這項限制是已知問題,因為調整限制。 Microsoft工程小組正致力於最終解決這項限制的新服務。

注意

執行大量作業,例如匯入或建立時,如果大量作業未在一小時內完成,可能會遇到問題。 若要解決此問題,建議您分割每個批次處理的記錄數目。 例如,在啟動匯出之前,您可以篩選群組類型或使用者名稱來限制結果集,以減少結果的大小。 藉由精簡篩選,基本上您會限制大量作業傳回的數據。

此問題的另一個因應措施是使用PowerShell直接Microsoft Graph API呼叫。 針對大量下載使用者和群組失敗,我們建議使用 PowerShell Cmdlet GET-MgGroup -AllGET-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 檔案包含必要的資料列(例如、DisplayNameUserPrincipalName、 等等)。 此外,請調整文本,以符合 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"  

下一步