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

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

作者:精品下载站 日期:2024-12-14 05:52:24 浏览:14 分类:玩电脑

如何使用 PowerShell 分配 Microsoft 365 许可证


您可以在 Microsoft 365 管理中心或 Microsoft Entra 管理中心中手动将许可证分配给单个或多个用户。如果您想要向批量用户分配许可证,创建 CSV 文件并使用 PowerShell 脚本会更快。在本文中,您将了解如何在 Microsoft Entra ID 和 Microsoft Graph PowerShell 中分配用户许可证。

Microsoft 365 用户许可证

每个组织拥有不同的 Microsoft 365 产品,其中一个产品可能包含来自多个订阅的许可证。要将许可证分配给您的用户,您需要选择 Microsoft 365 产品(例如 Microsoft 365 E5 Developer)。

您可以通过不同方式将 Microsoft 许可证分配给用户:

  • Microsoft 365 管理中心
  • 微软 Entra 管理中心
  • 微软图形PowerShell

我们将仅向您展示如何在 Microsoft Entra 管理中心和 PowerShell 中向用户分配许可证。这是因为 Microsoft 365 管理中心有限制,一次最多可以分配 20 个用户。

如何在 Microsoft Entra ID 中分配 Microsoft 365 许可证

要将 Microsoft Entra ID 中的 Microsoft 365 许可证分配给 Microsoft Entra ID 中的单个用户或多个用户,请按照以下步骤操作。

将 Microsoft 365 许可证分配给 Microsoft Entra ID 中的单个用户

按照以下步骤将 Microsoft 365 许可证分配给 Microsoft Entra ID 中的用户:

  1. 登录 Microsoft Entra 管理中心
  2. 展开身份 > 用户 > 所有用户
  3. 选择用户

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 单击左窗格底部上的许可证

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 点击作业

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 选择许可证
  2. 选择许可选项

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 点击保存

注意:在将许可证分配给用户之前,您必须指定使用位置。否则,用户的许可证分配将失败。

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 向成员(Amanda Morgan) 的许可证分配成功,并且状态活动

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

将 Microsoft 365 许可证分配给 Microsoft Entra ID 中的多个用户

要将 Microsoft 365 许可证分配给批量用户,请按照以下步骤操作:

  1. 登录 Microsoft Entra 管理中心
  2. 展开身份>显示更多

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 展开计费 > 许可证
  2. 点击所有产品
  3. 选择许可计划

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 点击分配

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 点击添加用户和组

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 点击用户
  2. 选择用户
  3. 点击选择

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 检查列表中的用户
  2. 点击顶部或底部的分配选项

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 检查是否要启用所有选项
  2. 点击顶部或底部的查看 + 分配

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 检查一切
  2. 点击分配

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 许可证分配已成功分配给多个用户

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

如果要将 Microsoft 365 许可证分配给批量用户,最好创建组。了解如何使用基于组的许可分配 Microsoft 365 许可证。

如何使用 PowerShell 分配 Microsoft 365 许可证

要使用 Microsoft Graph PowerShell 将 Microsoft 365 许可证分配给单个用户或多个用户,请按照以下步骤操作。

连接到 Microsoft Graph PowerShell

在开始之前,您必须安装 Microsoft Graph PowerShell 模块。以管理员身份启动 Windows PowerShell 并运行以下命令。

Install-Module Microsoft.Graph -Force

重要提示:在运行 cmdlet 或脚本之前,请务必安装最新的 Microsoft Graph PowerShell 模块版本,以防止出现错误和不正确的结果。

使用以下范围运行 Connect-MgGraph cmdlet 以通过 Microsoft Graph 进行身份验证。

Connect-MgGraph -Scopes "User.ReadWrite.All", "Organization.Read.All"

现在一切就绪,您可以通过 MS Graph PowerShell 使用命令。

获取 SKU ID 许可证

要使用 Microsoft Graph PowerShell 将许可证分配给多个用户,您需要知道许可证的 SKU ID。我们将使用 Get-MgSubscribedSku PowerShell cmdlet 获取显示您的组织可用许可计划的 SkuPartNumber。例如,ENTERPRISEPACKOffice 365 Enterprise E3 的许可证计划名称。

运行以下 PowerShell 命令以获取组织中所有许可证的 SKU ID

Get-MgSubscribedSku | Select-Object SkuPartNumber, SkuId

PowerShell 输出结果显示 SKU ID 许可证列表。

SkuPartNumber    SkuId
-------------    -----
DEVELOPERPACK_E5 c42b9cae-ea4f-4ab7-9717-81576235ccac

获取未授权用户

要查找组织中所有未经许可的用户帐户,您需要使用 Get-MgUser cmdlet。

运行以下 PowerShell 命令以获取所有未经许可的用户(不包括来宾)。

Get-MgUser -All -Filter "assignedLicenses/`$count eq 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable unlicensedUserCount

它将显示所有未经许可的用户帐户的列表。

使用 Microsoft Graph PowerShell 将 Microsoft 365 许可证分配给单个用户

要将许可证分配给特定用户,您将使用 Set-MgUserLicense PowerShell cmdlet。在我们的示例中,我们希望为用户 Amanda Morgan 分配 DEVELOPERPACK_E5 许可证。

注意:即使您不想删除任何许可证,也必须添加带有空数组 @()-RemoveLicenses 参数。

  1. 第 1 行中指定许可证 SKU ID
  2. 第 2 行中指定UserPrincipalName
  3. 运行以下 PowerShell 脚本
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "DEVELOPERPACK_E5" }
$user = Get-MgUser | Where-Object { $_.UserPrincipalName -eq "[email protected]" }
Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = ($license.SkuId) } -RemoveLicenses @()

PowerShell 输出显示已将许可证分配给该用户。

DisplayName   Id                                   Mail                       UserPrincipalName
-----------   --                                   ----                       -----------------
Amanda Morgan f8261d51-3df9-4f21-a6b1-533412669c11 [email protected] [email protected]

使用 Microsoft Graph PowerShell 将 Microsoft 365 许可证分配给多个用户

要将 Microsoft 365 许可证分配给多个用户,请使用以下 PowerShell 脚本:

  1. 第 2 行中指定用户的UserPrincipalName
  2. 第 5 行中指定许可证 SKU ID
  3. 运行以下 PowerShell 脚本
$users = @(
    "[email protected]",
    "[email protected]"
)
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "DEVELOPERPACK_E5" }

foreach ($user in $users) {
    $user = Get-MgUser -Filter "UserPrincipalName eq '$user'"
    try {
        $userLicense = Get-MgUserLicenseDetail -UserId $user.Id | Where-Object { $_.SkuId -eq $license.SkuId }
        if ($userLicense) {
            Write-Host "License $($license.SkuPartNumber) is already assigned to $($user.DisplayName)" -ForegroundColor Yellow
        }
        else {
            Write-Host "Assign license $($license.SkuPartNumber) to $($user.DisplayName)" -ForegroundColor Green
            $null = Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = ($license.SkuId) } -RemoveLicenses @() -ErrorAction Stop
        }
    }
    catch {
        Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
        break
    }
}

PowerShell 输出显示以下时间:

  • 许可证已分配给用户
  • 为用户分配许可证

PowerShell 脚本将在以下情况下停止:

  • 用户没有使用位置值
  • 没有更多可用许可证(已达到最大限制)
  • 用户不存在或拼写错误

它显示了以下 PowerShell 结果示例。

Assign license DEVELOPERPACK_E5 to Amanda Hansen
License DEVELOPERPACK_E5 is already assigned to Amanda Morgan

您还可以使用 Get-MgUser cmdlet 指定一组用户,如下例所示。

  1. 第 1 行中指定用户
  2. 第 2 行中指定许可证 SKU ID
  3. 运行以下 PowerShell 脚本
$users = Get-MgUser -All | Where-Object { $_.Mail -match "@a-d.site" }
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "DEVELOPERPACK_E5" }

foreach ($user in $users) {
    try {
        $userLicense = Get-MgUserLicenseDetail -UserId $user.Id | Where-Object { $_.SkuId -eq $license.SkuId }
        if ($userLicense) {
            Write-Host "License $($license.SkuPartNumber) is already assigned to $($user.DisplayName)" -ForegroundColor Yellow
        }
        else {
            Write-Host "Assign license $($license.SkuPartNumber) to $($user.DisplayName)" -ForegroundColor Green
            $null = Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = ($license.SkuId) } -RemoveLicenses @() -ErrorAction Stop
        }
    }
    catch {
        Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
        break
    }
}

PowerShell 输出显示以下时间:

  • 许可证已分配给用户
  • 为用户分配许可证

PowerShell 脚本将在以下情况下停止:

  • 用户没有使用位置值
  • 没有更多可用许可证(已达到最大限制)

它显示了以下 PowerShell 示例。

License DEVELOPERPACK_E5 is already assigned to Amanda Hansen
License DEVELOPERPACK_E5 is already assigned to Amanda Morgan
Assign license DEVELOPERPACK_E5 to Blake Martin
Assign license DEVELOPERPACK_E5 to Carl Hawk
An error occurred: [Request_BadRequest] : Subscription with SKU c42b9cae-ea4f-4ab7-9717-81576235ccac does not have any available licenses.

在 PowerShell 中通过 CSV 文件将 Microsoft 365 许可证分配给批量用户

您还可以创建一个 CSV 文件,其中一列中包含未许可的用户,并使用 PowerShell 脚本分配 Microsoft 365 许可证。

要将 Microsoft 365 许可证批量分配给用户,请按照以下步骤操作:

  1. 打开Microsoft Excel
  2. 输入 ID 作为第一列的标题
  3. 指定UserPrincipalName

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

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

[玩转系统] 如何使用 PowerShell 分配 Microsoft 365 许可证

  1. 第 1 行中输入CSV 文件路径
  2. 第 2 行中指定许可证 SKU ID
  3. 运行以下 PowerShell 脚本
$Users = Import-Csv "C:\temp\Users.csv"
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "DEVELOPERPACK_E5" }

foreach ($user in $users) {
    $userId = $user.Id
    $userObject = Get-MgUser -Filter "UserPrincipalName eq '$($user.Id)'"
    if ($userObject) {
        try {
            $userLicense = Get-MgUserLicenseDetail -UserId $userObject.Id | Where-Object { $_.SkuId -eq $license.SkuId }
            if ($userLicense) {
                Write-Host "License $($license.SkuPartNumber) is already assigned to $($userObject.DisplayName)" -ForegroundColor Yellow
            }
            else {
                Write-Host "Assign license $($license.SkuPartNumber) to $($userObject.DisplayName)" -ForegroundColor Green
                $null = Set-MgUserLicense -UserId $userObject.Id -AddLicenses @{SkuId = ($license.SkuId) } -RemoveLicenses @() -ErrorAction Stop
            }
        }
        catch {
            Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
            break
        }
    }
    else {
        Write-Host "User $userId does not exist" -ForegroundColor Red
    }
}

PowerShell 输出显示以下时间:

  • 许可证已分配给用户
  • 为用户分配许可证
  • 用户不存在或拼写错误

PowerShell 脚本将在以下情况下停止:

  • 用户没有使用位置值
  • 没有更多可用许可证(已达到最大限制)
Assign license DEVELOPERPACK_E5 to Anne Butler
License DEVELOPERPACK_E5 is already assigned to Anna Bell
License DEVELOPERPACK_E5 is already assigned to Frank Olsen
User [email protected] does not exist
License DEVELOPERPACK_E5 is already assigned to Ken Walker
Assign license DEVELOPERPACK_E5 to Stephen Hunter
An error occurred: [Request_BadRequest] : License assignment cannot be done for user with invalid usage location.

就是这样!

了解更多:更改 Microsoft 365 主 SMTP 地址 »

结论

您了解了如何在 Microsoft Graph PowerShell 和 Microsoft Entra ID 中分配 Microsoft 365 许可证。使用这两种方法,都可以将许可证分配给单个和多个用户。您可以在 PowerShell 中使用多种变体将 Microsoft 365 许可证分配给批量用户。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 将联系人批量导入到 Microsoft 365。不要忘记关注我们并分享这篇文章。

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

取消回复欢迎 发表评论:

关灯