检查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

重要的:在运行CMDLET或脚本以防止错误和错误结果之前,请务必更新到最新的Microsoft Graph PowerShell模块版本。

步骤2。准备Get-LacenSeassignment PowerShell脚本

(c :)驾驶:

  • 温度
  • 脚本

下载get-licenseassignment.ps1 powershell脚本并将其放入C:脚本文件夹。脚本将将CSV文件导出到C:温度文件夹。

确保文件未阻止以防止运行脚本时的错误。在运行PowerShell脚本时,请在文章中阅读更多信息。

另一个选择是将下面的代码复制并粘贴到记事本中。给它名字get-licenseassignment.ps1并将其放在C:脚本文件夹。

<#
    .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", "Group.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 ($null -ne $user.AssignedByGroup) {
        $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:tempLicenseAssignments.csv" -NoTypeInformation -Encoding utf8
$Report | Out-GridView -Title "License Assignments"
  • 第58行:编辑CSV文件路径

步骤3。运行get-lacenSeassignment 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

一个离格视图将显示您需要的所有信息。

步骤4。开放许可分配报告

get-licenseassignment.ps1 powershell脚本将所有Microsoft 365用户许可分配导出到CSV文件。

更多阅读:删除具有组许可用户的直接分配许可证

查找文件licenSeassignments.csv在路径中C:温度

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

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

笔记:建议通过基于组的许可分配Microsoft 365许可证,而不是直接的许可证,以便您可以轻松管理它。

结论

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

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