[玩转系统] 使用 Microsoft Graph PowerShell 更新 Azure AD 用户
作者:精品下载站 日期:2024-12-14 09:15:02 浏览:15 分类:玩电脑
使用 Microsoft Graph PowerShell 更新 Azure AD 用户
有时,您需要添加或更改单个或多个 Azure AD 用户信息。我们将向您展示如何更新单个 Azure AD 用户的一个属性和多个属性。此外,我们将为所有 Azure AD 用户批量设置多个属性。在本文中,你将了解如何使用 Microsoft Graph PowerShell 更新 Azure AD 用户。
已弃用 Set-AzureADUser 和 Set-MsolUser
Microsoft 宣布将于 2024 年 3 月 30 日弃用 Set-AzureADUser 和 Set-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"
现在,您可以将 Update-MgUser 和 Update-MgBetaUser cmdlet 与 Microsoft Graph PowerShell 结合使用。
注意:并非所有用户帐户属性都可以由成员或来宾用户在没有管理员角色的情况下使用其默认权限进行更新。
更新单个 Azure AD 用户属性
我们将演示如何使用 Microsoft Graph PowerShell 更改单个 Azure AD 用户属性。然后,我们将展示如何更新单个 Azure AD 用户的多个属性。
Update-MgBetaUser Azure AD 属性
要更新 Azure AD 用户帐户信息,我们将使用 Update-MgBetaUser cmdlet。
您可以添加单个用户的国家/地区。在我们的示例中,用户 Amanda Hansen 的信息详细信息中没有设置国家/地区。
运行以下 PowerShell 命令示例以添加用户国家/地区。
Update-MgBetaUser -UserId "[email protected]" -Country "GB"
它会自动添加 Azure AD 用户的国家/地区信息。
更改单个 Azure AD 用户属性
如果用户更改到不同的部门,您可能需要为该用户进行编辑。我们将使用-Department参数添加新信息。
在我们的示例中,我们希望将用户 [email protected] 从销售部门更改为营销部门。
运行以下 PowerShell 命令示例。
Update-MgBetaUser -UserId "[email protected]" -Department "Marketing"
它会立即更新单个 Azure AD 用户的作业信息。
更新单个 Azure AD 用户的多个属性
假设您想要更新特定 Azure AD 用户的属性,因为该用户有另一个职位和新的电话号码。
在我们的示例中,我们将使用 -BusinessPhones 参数添加号码,并使用 -JobTitle 参数更改用户 Amanda Hansen 的工作。
$UserId = (Get-MgUser -UserId "[email protected]").Id
Update-MgBetaUser -UserId $UserId -JobTitle "Marketing Manager" -BusinessPhone "+44 20 8885 6673"
为了检查用户属性是否正确更新,我们需要获取用户详细信息。
Get-MgBetaUser -UserId "[email protected]" | Select DisplayName, BusinessPhones, JobTitle, Mail, City, CompanyName, Department, EmployeeId, StreetAddress, Country
PowerShell 输出结果将显示您添加了电话号码并更改了职位名称。
PS C:\> Get-MgBetaUser -UserId "[email protected]" | Select DisplayName, BusinessPhones, JobTitle, Mail, City, CompanyName, Department, EmployeeId, StreetAddress, Country
DisplayName : Amanda Hansen
BusinessPhones : {+44 20 8885 6673}
JobTitle : Marketing Manager
Mail : [email protected]
City : London
CompanyName : NewCompany
Department : Marketing
EmployeeId : 123
StreetAddress : X
Country : GB
更新多个 Azure AD 用户的属性
我们将使用 CSV 文件更改多个或所有 Azure AD 用户的属性。我们还将向您展示如何从 CSV 文件为所有 Azure AD 用户批量设置多个属性。
更新多个 Azure AD 用户的职务
我们想要更改和设置多个 Azure AD 用户的职位。首先,创建一个新的 CSV 文件,其中包含要更新的所有 Azure AD 用户。
打开 Microsoft Excel 并输入以下数据来更改和设置职位:
- 在 A 列顶部输入 UserPrincipalName
- 在 B 列顶部输入 JobTitle
- 在 A 列下列出 Azure AD 用户
- 在 B 列下列出每个用户的新职位
CSV 文件应如下例所示。
- 在(C:)驱动器上创建temp文件夹
- 将文件命名为 UsersJob.csv
- 保存类型为 CSV UTF-8(逗号分隔)(*.csv)
- 点击保存
注意:是否输入了值或值为空 (null) 并不重要,因为 CSV 中的所有内容都会覆盖它。
- 运行以下 PowerShell 脚本
# Connect to Microsoft Graph
Connect-MgGraph -Scope User.ReadWrite.All
# Read the CSV file
$users = Import-Csv -Path "C:\temp\UsersJob.csv"
# Go through each user in the CSV and update the job title
foreach ($user in $users) {
$userPrincipalName = $user.UserPrincipalName
$jobTitle = $user.JobTitle
# Check if the user exists
$existingUser = Get-MgBetaUser -UserId $userPrincipalName -ErrorAction SilentlyContinue
if ($existingUser) {
# Check if the existing job title matches the new value
if ($existingUser.JobTitle -eq $jobTitle) {
# Job title already set with the same value
Write-Host "User '$userPrincipalName' already has job title '$jobTitle'." -ForegroundColor Cyan
}
else {
# Update the job title
Update-MgBetaUser -UserId $userPrincipalName -JobTitle $jobTitle
Write-Host "User '$userPrincipalName' updated job title to '$jobTitle' successfully." -ForegroundColor Green
}
}
else {
# User not found
Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
}
}
PowerShell输出结果显示如下:
- Azure AD 用户更新了新职位名称
- Azure AD 用户已设置相同的职位名称
- 在 Azure AD 中找不到的未知用户
User '[email protected]' updated job title to 'Manager' successfully.
User '[email protected]' already has job title 'Trainee'.
User '[email protected]' updated job title to 'Chef' successfully.
User '[email protected]' updated job title to 'Team Leader' successfully.
User '[email protected]' not found.
更新多个 Azure AD 用户的国家/地区
我们想要更改和设置多个或所有 Azure AD 用户的国家/地区。首先,创建一个新的 CSV 文件,其中包含要更新的所有 Azure AD 用户。
打开 Microsoft Excel 并输入以下数据:
- 在顶部 A 列中输入 UserPrincipalName
- 在 B 列顶部输入国家/地区
- 在 A 列下列出 Azure AD 用户
- 在 B 列下列出每个用户的新国家/地区
请参阅下面的 CSV 文件示例。
- 在(C:)驱动器上创建temp文件夹
- 将文件命名为 UsersCountry.csv
- 保存类型为 CSV UTF-8(逗号分隔)(*.csv)
- 点击保存
注意:如果国家/地区的值为空 (null) 或为某些用户写入了值,则 CSV 的新值将替换它们。
- 运行以下 PowerShell 脚本
# Connect to Microsoft Graph
Connect-MgGraph -Scope User.ReadWrite.All
# Read the CSV file
$users = Import-Csv -Path "C:\temp\UsersCountry.csv"
# Go through each user in the CSV and update the country
foreach ($user in $users) {
$userPrincipalName = $user.UserPrincipalName
$country = $user.Country
# Check if the user exists
$existingUser = Get-MgBetaUser -UserId $userPrincipalName -ErrorAction SilentlyContinue
if ($existingUser) {
# Check if the existing country matches the new value
if ($existingUser.Country -eq $country) {
# Country already set with the same value
Write-Host "User '$userPrincipalName' already has country '$country'." -ForegroundColor Cyan
}
else {
# Update the country
Update-MgBetaUser -UserId $userPrincipalName -Country $country
Write-Host "User '$userPrincipalName' updated country to '$country' successfully." -ForegroundColor Green
}
}
else {
# User not found
Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
}
}
PowerShell输出结果显示如下:
- Azure AD 用户更新了新国家/地区
- Azure AD 用户已设置相同的国家/地区
- 在 Azure AD 中找不到的未知用户
User '[email protected]' updated country to 'US' successfully.
User '[email protected]' already has country 'US'.
User '[email protected]' updated country to 'Germany' successfully.
User '[email protected]' updated country to 'DK' successfully.
User '[email protected]' not found.
批量更新 Azure AD 用户的多个属性
假设您要更改并设置所有 Azure AD 用户的国家/地区、员工 ID 和职位。您需要为要更新的所有用户创建一个 CSV 文件。
注意:一个很好的方法是首先导出所有 Azure AD 用户,并使用要更新的用户的新属性调整 CSV 文件。完成后,您可以跳过以下步骤并直接转到下面的 PowerShell 脚本。
打开 Microsoft Excel 并输入以下数据:
- 在顶部 A 列中输入 UserPrincipalName
- 在 B 列顶部输入国家/地区
- 在 C 列顶部输入 EmployeeId
- 在 D 列顶部输入 JobTitle
- 列出A 列下的Azure AD 用户
- 在B 列下列出每个用户的新国家/地区
- 在C 列下列出每个用户的新员工 ID
- 在D 列下列出每个用户的新职位
请参阅下面的 CSV 文件示例。
- 在(C:)驱动器上创建temp文件夹
- 将文件命名为 UsersProperties.csv
- 保存类型为 CSV UTF-8(逗号分隔)(*.csv)
- 点击保存
注意:如果用户属性的值为空 (null) 或为某些用户写入了值,则 CSV 的新值将替换它们。
- 运行以下 Powershell 脚本
# Connect to Microsoft Graph
Connect-MgGraph -Scope User.ReadWrite.All
# Read the CSV file
$users = Import-Csv -Path "C:\temp\UsersProperties.csv"
# Go through each user in the CSV and update the properties
foreach ($user in $users) {
$userPrincipalName = $user.UserPrincipalName
$employeeId = $user.EmployeeId
$jobTitle = $user.JobTitle
$country = $user.Country
# Check if the user exists
$existingUser = Get-MgBetaUser -UserId $userPrincipalName -ErrorAction SilentlyContinue
if ($existingUser) {
# Check if the existing properties match the new values
$updateNeeded = $false
if ($existingUser.EmployeeId -ne $employeeId) {
$existingUser.EmployeeId = $employeeId
$updateNeeded = $true
}
if ($existingUser.JobTitle -ne $jobTitle) {
$existingUser.JobTitle = $jobTitle
$updateNeeded = $true
}
if ($existingUser.Country -ne $country) {
$existingUser.Country = $country
$updateNeeded = $true
}
if ($updateNeeded) {
# Update the user properties
Update-MgBetaUser -UserId $userPrincipalName -EmployeeId $employeeId -JobTitle $jobTitle -Country $country
Write-Host "User '$userPrincipalName' updated successfully." -ForegroundColor Green
}
else {
Write-Host "User '$userPrincipalName' properties are up to date." -ForegroundColor Cyan
}
}
else {
# User not found
Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
}
}
PowerShell输出结果显示如下:
- Azure AD 用户更新了新的国家/地区、员工 ID 或职位名称
- Azure AD 用户已设置这些属性
- 在 Azure AD 中找不到的未知用户
User '[email protected]' properties are up to date.
User '[email protected]' updated successfully.
User '[email protected]' updated successfully.
User '[email protected]' updated successfully.
User '[email protected]' not found.
你了解了如何使用 MS Graph PowerShell 更新 Azure AD 用户属性。
了解更多:为 Azure AD 用户设置员工 ID »
结论
您了解了如何使用 Update-MgUser cmdlet 通过 Microsoft Graph PowerShell 更新 Azure AD 用户。您可以使用 PowerShell 更改单个用户的多个属性。还可以使用 CSV 文件为所有 Azure AD 用户批量设置属性。
您喜欢这篇文章吗?您可能还喜欢在 Azure 中使用基于组的许可分配 Microsoft 365 许可证。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 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