网站搜索

检查 Microsoft 365 用户许可证是直接分配还是从组继承


有两种分配 Microsoft 365 许可证的方法。一种方法是直接分配许可证,另一种是将用户添加到链接到许可证的组中,称为基于组的许可。更好的方法是基于组的许可,因为它更容易维护和用于审计目的。在本文中,您将了解如何检查 Microsoft 365 用户许可证是直接分配还是从组继承。

使用 PowerShell 检查用户许可证分配类型

要检查 Microsoft 365 用户许可证是直接分配的还是从使用 Microsoft Graph PowerShell 的组继承的,请按照以下步骤操作:

步骤 1. 安装 Microsoft Graph PowerShell

以管理员身份运行 Windows PowerShell 并安装 Microsoft Graph PowerShell。

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

重要提示:始终安装 Microsoft Graph PowerShell 和 Microsoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。

步骤 2. 准备 Get-LicenseAssignment PowerShell 脚本

在(C:)驱动器上创建两个文件夹:

  • 温度
  • 脚本

下载 Get-LicenseAssignment.ps1 PowerShell 脚本并将其放置在 C:\scripts 文件夹中。该脚本会将 CSV 文件导出到 C:\temp 文件夹。

确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Get-LicenseAssignment.ps1 并将其放置在 C:\scripts 文件夹中。

<#
    .SYNOPSIS
    Get-LicenseAssignment.ps1

    .DESCRIPTION
    The script will export all Microsoft 365 users license assignment to CSV file.

    .LINK
    www.alitajran.com/microsoft-365-user-license-direct-assigned-or-inherited-from-group/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.alitajran.com
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 03/04/2024 - Initial version
#>

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All"

# Get all users using Get-MgUser with a filter
$users = Get-MgUser -All -Property AssignedLicenses, LicenseAssignmentStates, DisplayName |
Select-Object DisplayName, AssignedLicenses -ExpandProperty LicenseAssignmentStates

$Report = [System.Collections.Generic.List[Object]]::new()

# Loop through all users and get the AssignedByGroup Details which will list the groupId
foreach ($user in $users) {
    # Get the group ID if AssignedByGroup is not empty
    if ($user.AssignedByGroup -ne $null) {
        $groupId = $user.AssignedByGroup
        $groupName = Get-MgGroup -GroupId $groupId | Select-Object -ExpandProperty DisplayName
        Write-Host "$($user.DisplayName) is assigned by group - $($groupName)" -ErrorAction SilentlyContinue -ForegroundColor Cyan
        $ReportLine = [PSCustomObject]@{
            User      = $user.DisplayName
            Assigned  = "Group"
            GroupName = $groupName
            GroupId   = $groupId
        }
        $Report.Add($ReportLine)
    }

    else {
        $ReportLine = [PSCustomObject]@{
            User      = $user.DisplayName
            Assigned  = "Direct"
            GroupName = "N/A"
            GroupId   = "N/A"
        }
        $Report.Add($ReportLine)
        Write-Host "$($user.DisplayName) is direct assigned" -ErrorAction SilentlyContinue -ForegroundColor Yellow
    }
}

# Display the results
$Report | Export-Csv -Path "C:\temp\LicenseAssignments.csv" -NoTypeInformation -Encoding utf8
$Report | Out-GridView -Title "License Assignments"
  • 第 58 行:编辑 CSV 文件路径

步骤 3. 运行 Get-LicenseAssignment PowerShell 脚本

运行以下命令来运行脚本 Get-LicenseAssignment.ps1。

c:\scripts\.\Get-LicenseAssignment.ps1

PowerShell 输出显示许可证是由组继承还是直接分配。

注意:输出显示是否有多个组分配给用户,或者用户是否同时拥有直接分配和按组分配的许可证。如果两者都分配给用户,请阅读文章删除为具有组许可证的用户直接分配的许可证。

Alison Bell is assigned by group - O365_Licenses_E3_Base
Alison Bell is assigned by group - O365_Licenses_E3_Exchange
Boris Campbell is assigned by group - O365_Licenses_E3_Exchange
Carol Baker is assigned by group - O365_Licenses_E3_Exchange
CloudOnly is direct assigned
Edward Lincoln is assigned by group - O365_Licenses_E3_Exchange
Jeffrey Welch is direct assigned
Jérôme User is direct assigned
Lauren Russell is direct assigned
Phil Peters is direct assigned
Richard Grant is assigned by group - O365_Licenses_E3_Exchange
Richard Grant is direct assigned
Zoë Roberts is direct assigned
Zoë Roberts is assigned by group - O365_Licenses_E3_Exchange

Out-GridView 将显示您需要的所有信息。

步骤 4. 打开许可证分配报告

Get-LicenseAssignment.ps1 PowerShell 脚本将所有 Microsoft 365 用户许可证分配导出到 CSV 文件。

在路径 C:\temp 中找到文件 LicenseAssignments.csv。

使用您喜欢的应用程序打开 CSV 文件。在我们的示例中,它是 Microsoft Excel。

Microsoft 365 用户许可证分配类型报告看起来很棒。

注意:建议使用基于组的许可而不是直接分配 Microsoft 365 许可证,以便您可以轻松管理。

了解更多:使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户 »

结论

您了解了如何检查 Microsoft 365 用户许可证是直接分配还是从组继承。使用 PowerShell 是检索许可证信息的最佳方法。

您喜欢这篇文章吗?您可能还喜欢导出 Microsoft 365 非活动用户报告。不要忘记关注我们并分享这篇文章。

相关文章