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

[玩转系统] 使用 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-AzureADUserSet-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 并输入以下数据来更改和设置职位:

  1. 在 A 列顶部输入 UserPrincipalName
  2. 在 B 列顶部输入 JobTitle
  3. 在 A 列下列出 Azure AD 用户
  4. 在 B 列下列出每个用户的新职位

CSV 文件应如下例所示。

[玩转系统] 使用 Microsoft Graph PowerShell 更新 Azure AD 用户

  1. (C:)驱动器上创建temp文件夹
  2. 将文件命名为 UsersJob.csv
  3. 保存类型为 CSV UTF-8(逗号分隔)(*.csv)
  4. 点击保存

[玩转系统] 使用 Microsoft Graph PowerShell 更新 Azure AD 用户

注意:是否输入了值或值为空 (null) 并不重要,因为 CSV 中的所有内容都会覆盖它。

  1. 运行以下 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 并输入以下数据:

  1. 在顶部 A 列中输入 UserPrincipalName
  2. 在 B 列顶部输入国家/地区
  3. 在 A 列下列出 Azure AD 用户
  4. 在 B 列下列出每个用户的新国家/地区

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

[玩转系统] 使用 Microsoft Graph PowerShell 更新 Azure AD 用户

  1. (C:)驱动器上创建temp文件夹
  2. 将文件命名为 UsersCountry.csv
  3. 保存类型为 CSV UTF-8(逗号分隔)(*.csv)
  4. 点击保存

[玩转系统] 使用 Microsoft Graph PowerShell 更新 Azure AD 用户

注意:如果国家/地区的值为空 (null) 或为某些用户写入了值,则 CSV 的新值将替换它们。

  1. 运行以下 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 并输入以下数据:

  1. 在顶部 A 列中输入 UserPrincipalName
  2. 在 B 列顶部输入国家/地区
  3. 在 C 列顶部输入 EmployeeId
  4. 在 D 列顶部输入 JobTitle
  5. 列出A 列下的Azure AD 用户
  6. B 列下列出每个用户的新国家/地区
  7. C 列下列出每个用户的新员工 ID
  8. D 列下列出每个用户的新职位

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

[玩转系统] 使用 Microsoft Graph PowerShell 更新 Azure AD 用户

  1. (C:)驱动器上创建temp文件夹
  2. 将文件命名为 UsersProperties.csv
  3. 保存类型为 CSV UTF-8(逗号分隔)(*.csv)
  4. 点击保存

[玩转系统] 使用 Microsoft Graph PowerShell 更新 Azure AD 用户

注意:如果用户属性的值为空 (null) 或为某些用户写入了值,则 CSV 的新值将替换它们。

  1. 运行以下 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 许可证。不要忘记关注我们并分享这篇文章。

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

取消回复欢迎 发表评论:

关灯