当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 Microsoft Graph PowerShell 恢复 Azure AD 用户

作者:精品下载站 日期:2024-12-14 09:15:02 浏览:15 分类:玩电脑

使用 Microsoft Graph PowerShell 恢复 Azure AD 用户


最近删除的 Azure AD 用户将在 Azure AD 回收站中保留 30 天。在这 30 天内,您可以使用 Restore-MgDirectoryDeletedItem cmdlet 完全还原 Azure AD 用户帐户。如果创建 CSV 文件,则可以批量还原已删除的 Azure AD 用户。在本文中,你将了解如何使用 Microsoft Graph PowerShell 恢复 Azure AD 用户。

恢复 MsolUser 已弃用

Microsoft 宣布将于 2024 年 3 月 30 日弃用 Restore-MsolUser cmdlet。您需要将 MS Online PowerShell 模块替换为 Microsoft Graph PowerShell。要恢复 Azure AD 用户,我们将使用 Microsoft Graph PowerShell cmdlet。

使用 Microsoft Graph PowerShell 管理 Azure AD 用户

我们创建了具体的文章来使用 Microsoft Graph PowerShell 管理 Azure AD 用户:

  • 使用 Microsoft Graph PowerShell 导出 Azure AD 用户
  • 使用 Microsoft Graph PowerShell 删除 Azure AD 用户
  • 使用 Microsoft Graph PowerShell 还原 Azure AD 用户(本文)
  • 使用 Microsoft Graph PowerShell 更新 Azure AD 用户

连接到 Microsoft Graph PowerShell

在开始之前,必须安装 Microsoft Graph PowerShell 模块,包括 Microsoft Graph Beta 模块。

运行以下命令来安装 Microsoft Graph 模块。

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

您还需要使用以下权限连接到 MS Graph。

Connect-MgGraph -Scopes "User.Read.All", "User.ReadWrite.All", "Directory.AccessAsUser.All", "Directory.ReadWrite.All"

现在您已准备好使用 Microsoft Graph PowerShell 命令。

将软删除的 Azure AD 用户列表批量导出到 CSV

您可以获取回收站中所有 Azure AD 已删除用户的列表。下面的脚本将在网格视图中显示列表并保存 CSV 文件。要将列表批量导出到 CSV 文件,请创建一个 temp 文件夹并将其保存在 (C:) 驱动器中。

运行以下 PowerShell 脚本。

# Retrieve deleted directory items
$DeletedItems = Get-MgDirectoryDeletedItem -DirectoryObjectId Microsoft.Graph.User -Property 'id', 'displayName'

# Check if there are no deleted accounts
if ($DeletedItems.AdditionalProperties['value'].Count -eq 0) {
    Write-Host "No deleted accounts found - exiting"
}
else {
    # Create an array to store the report
    $Report = @()

    # Loop through the deleted items
    foreach ($Item in $DeletedItems.AdditionalProperties['value']) {
        $ReportLine = [PSCustomObject]@{
            UserId              = $Item['id']
            Name                = $Item['displayName']
        }
        $Report += $ReportLine
    }

    # Sort and export the report by 'UserId'
    $Report | Sort-Object UserId | Select-Object UserId, Name | Out-GridView
    $Report | Sort-Object UserId | Export-Csv -Path "C:\temp\AzureDeletedUsers.csv" -Encoding UTF8 -NoTypeInformation
}

您将在 C:\temp 文件夹中找到 CSV 文件。使用 Microsoft Excel 等应用程序打开 CSV 文件以查看结果。这些是 Azure AD 回收站中所有已删除的用户。

[玩转系统] 使用 Microsoft Graph PowerShell 恢复 Azure AD 用户

您可以使用此 CSV 文件来恢复某些 Azure AD 用户。

恢复 Azure AD 用户

删除 Azure AD 用户时,该帐户会移至 Azure AD 回收站并保留 30 天。仅当用户仍位于 Azure AD 回收站中时,才能还原已删除的用户。我们将向您展示如何通过三种方法恢复已删除的 Azure AD 用户帐户:

  1. 还原单个 Azure AD 用户帐户
  2. 使用 CSV 还原多个 Azure AD 用户
  3. 批量还原所有 Azure AD 用户

1. Restore-MgDirectoryDeletedItem Azure AD 用户

假设您犯了一个错误并且想要恢复相同的用户帐户。不幸的是,您无法使用 Restore-MgUser cmdlet,因为它不起作用。因此,我们需要将 Restore-MgDirectoryDeletedItem 与 -DirectoryObjectId 参数一起使用。

注意: Restore-MgUser cmdlet 不存在。

在我们的示例中,我们要删除用户帐户[email protected]。恢复用户帐户时,必须使用对象 ID 编号。

运行以下 PowerShell 命令示例。

Restore-MgDirectoryDeletedItem -DirectoryObjectId "a9532b30-4edb-4b66-a3b0-6ac972a6065b"

PowerShell 输出显示以下结果。

PS C:\> Restore-MgDirectoryDeletedItem -DirectoryObjectId "a9532b30-4edb-4b66-a3b0-6ac972a6065b"

Id                                   DeletedDateTime
--                                   ---------------
a9532b30-4edb-4b66-a3b0-6ac972a6065b                

2. 从 CSV 恢复多个 Azure AD 用户

要从回收站恢复一些 Azure AD 用户,您需要创建一个 CSV 文件。我们需要使用 Get-MgDirectoryDeletedItem cmdlet 来获取 Azure AD 回收站中所有已删除的用户。此外,我们将使用 Restore-MgDirectoryDeletedItem cmdlet 从 CSV 文件恢复用户。

注意:您需要提供带有用户帐户连字符 (-) 的ID 号,否则 PowerShell 脚本将无法运行。我们还将列出每个用户的 UPN,以便清楚地了解每个ID 号所属的人。

创建一个包含 2 列的 CSV 文件:

  1. 在第一列顶部输入ID
  2. 在第二列顶部输入 UserPrincipalName
  3. ID 下列出每个用户的ID 号,包括连字符 (-)
  4. UserPrincipalName 下列出每个用户的 UPN

请参阅下面的 CSV 文件示例。

[玩转系统] 使用 Microsoft Graph PowerShell 恢复 Azure AD 用户

  1. 如果 (C:) 驱动器中还没有文件夹 temp,请创建该文件夹
  2. 将文件命名为 Restore.csv
  3. 保存类型为 CSV UTF-8(逗号分隔)(*.csv)
  4. 点击保存

[玩转系统] 使用 Microsoft Graph PowerShell 恢复 Azure AD 用户

  1. 运行以下 PowerShell 脚本以还原多个 Azure AD 用户
# Retrieve deleted directory items
$DeletedItems = Get-MgDirectoryDeletedItem -DirectoryObjectId Microsoft.Graph.User -Property 'Id', 'displayName'

# Import the CSV file with user IDs
$UserIds = Import-Csv -Path "C:\temp\Restore.csv"

foreach ($UserId in $UserIds) {
    $Id = $UserId.ID

    # Check if the user ID exists in the deleted items
    $DeletedUser = $DeletedItems.AdditionalProperties['value'] | Where-Object { $_['id'] -eq $Id }

    if ($DeletedUser -ne $null) {
        # Restore the user by ID
        Restore-MgDirectoryDeletedItem -DirectoryObjectId $Id -WhatIf
        Write-Host "Restored user $($DeletedUser['displayName']) with ID: $Id" -ForegroundColor Green
    }
    else {
        Write-Host "User with ID $Id not found in deleted items." -ForegroundColor Yellow
    }
}

Write-Host "Restoration process completed." -ForegroundColor Cyan
  1. 出现PowerShell输出结果
Restored user George Wilson with ID: 67962421-00e7-448b-b382-83b7b434e41c
Restored user Kelly Test with ID: 381ba50d-e356-4b5c-afd5-9157cd38d338
Restored user Laura Terry with ID: d601b084-c7df-4113-8089-2ca8d7303265
User with ID 954b27cf-8401-420b-bbd2-7f70903c0707 not found in deleted items.
Restoration process completed.

3.批量恢复所有软删除的Azure AD用户

您可以批量还原回收站中所有已删除的 Azure AD 用户。我们需要在脚本中使用 Get-MgDirectoryDeletedItemRestore-MgDirectoryDeletedItem cmdlet。

运行以下 PowerShell 脚本。

# Retrieve deleted directory items
$DeletedItems = Get-MgDirectoryDeletedItem -DirectoryObjectId Microsoft.Graph.User -Property 'Id', 'displayName'

# Check if there are no deleted accounts
if ($DeletedItems.AdditionalProperties['value'].count -eq 0) {
    Write-Host "No deleted accounts found." -ForegroundColor Cyan
}
else {
    # Restore deleted users
    foreach ($Item in $DeletedItems.AdditionalProperties['value']) {
        # Restore the user by ID
        Restore-MgDirectoryDeletedItem -DirectoryObjectId $Item['id'] -WhatIf
        Write-Host "Restored user $($Item['displayName'])" -ForegroundColor Green
    }
    
    Write-Host "Restoration process completed." -ForegroundColor Cyan
}

它将恢复 Azure AD 回收站中所有已删除的用户。

Restored user Ken Walker
Restored user Jill Bates
Restored user Megan Jones
Restoration process completed.

您已成功使用 Restore-MgDirectoryDeletedItem cmdlet 还原 Azure AD 用户。

了解更多:启用或禁用 Exchange ActiveSync 邮箱 »

结论

您了解了如何使用 Microsoft Graph PowerShell 恢复 Azure AD 用户。您可以将回收站中所有已删除的 Azure AD 用户批量导出到单个 CSV 文件。然后,可以使用 MS Graph PowerShell 从回收站还原单个、多个或所有 Azure AD 用户帐户。

您喜欢这篇文章吗?您可能还喜欢为 Microsoft 365 用户重置 MFA。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯