[玩转系统] 使用 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 回收站中所有已删除的用户。
您可以使用此 CSV 文件来恢复某些 Azure AD 用户。
恢复 Azure AD 用户
删除 Azure AD 用户时,该帐户会移至 Azure AD 回收站并保留 30 天。仅当用户仍位于 Azure AD 回收站中时,才能还原已删除的用户。我们将向您展示如何通过三种方法恢复已删除的 Azure AD 用户帐户:
- 还原单个 Azure AD 用户帐户
- 使用 CSV 还原多个 Azure AD 用户
- 批量还原所有 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 文件:
- 在第一列顶部输入ID
- 在第二列顶部输入 UserPrincipalName
- 在 ID 下列出每个用户的ID 号,包括连字符 (-)
- 在 UserPrincipalName 下列出每个用户的 UPN
请参阅下面的 CSV 文件示例。
- 如果 (C:) 驱动器中还没有文件夹 temp,请创建该文件夹
- 将文件命名为 Restore.csv
- 保存类型为 CSV UTF-8(逗号分隔)(*.csv)
- 点击保存
- 运行以下 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
- 出现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-MgDirectoryDeletedItem 和 Restore-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。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag