forked from jameswh3/MW-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-EntraUserLicenseInfo.ps1
More file actions
149 lines (121 loc) · 5.07 KB
/
Copy pathGet-EntraUserLicenseInfo.ps1
File metadata and controls
149 lines (121 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<#
.SYNOPSIS
Retrieves raw licensing information for an Entra ID user.
.DESCRIPTION
This script retrieves detailed licensing information for a specified Entra ID user,
including assigned license SKUs, service plans, and their status without mapping SKU IDs to friendly names.
.PARAMETER UserPrincipalName
The UserPrincipalName of the Entra ID user to query.
.PARAMETER OutputFormat
The format to output results. Valid values are 'Host' (default) or 'Object'.
Use 'Object' to return raw objects for pipeline processing.
.EXAMPLE
.\Get-EntraUserLicenseInfo.ps1 -UserPrincipalName "user@contoso.com"
Retrieves raw license information for the specified user and displays it.
.EXAMPLE
.\Get-EntraUserLicenseInfo.ps1 -UserPrincipalName "user@contoso.com" -OutputFormat Object | Export-Csv -Path "UserLicenses.csv" -NoTypeInformation
Retrieves raw license information and exports it to a CSV file.
.NOTES
Requires Microsoft Graph PowerShell SDK modules:
- Microsoft.Graph.Authentication
- Microsoft.Graph.Users
Required permissions:
- User.Read.All or Directory.Read.All
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$UserPrincipalName,
[Parameter(Mandatory = $false)]
[ValidateSet('Host', 'Object')]
[string]$OutputFormat = 'Host'
)
function Connect-MgGraphIfNeeded {
try {
$context = Get-MgContext -ErrorAction Stop
if (-not $context) {
throw "No existing connection found"
}
Write-Verbose "Using existing Microsoft Graph connection"
}
catch {
Write-Verbose "Connecting to Microsoft Graph..."
Connect-MgGraph -Scopes "User.Read.All", "Directory.Read.All" -ErrorAction Stop
}
}
function Format-ServicePlanStatus {
param (
[string]$Status
)
switch ($Status) {
"Success" { return "Enabled" }
"Disabled" { return "Disabled" }
"PendingInput" { return "Pending Input" }
"PendingActivation" { return "Pending Activation" }
"Error" { return "Error" }
default { return $Status }
}
}
# Main script execution
try {
# Check for required modules
$requiredModules = @("Microsoft.Graph.Authentication", "Microsoft.Graph.Users")
foreach ($module in $requiredModules) {
if (-not (Get-Module -ListAvailable -Name $module)) {
Write-Error "Required module $module is not installed. Please install it using: Install-Module $module -Scope CurrentUser"
return
}
}
# Connect to Microsoft Graph if not already connected
Connect-MgGraphIfNeeded
# Get user information
$user = Get-MgUser -UserId $UserPrincipalName -Property "displayName,userPrincipalName,assignedLicenses" -ErrorAction Stop
if (-not $user) {
Write-Error "User not found: $UserPrincipalName"
return
}
# Get detailed license information
$licenseDetails = Get-MgUserLicenseDetail -UserId $UserPrincipalName -ErrorAction Stop
if ($OutputFormat -eq 'Object') {
# Return license details as objects for pipeline processing
$results = @()
foreach ($license in $licenseDetails) {
foreach ($servicePlan in $license.ServicePlans) {
$results += [PSCustomObject]@{
UserPrincipalName = $user.UserPrincipalName
DisplayName = $user.DisplayName
SkuId = $license.SkuId
SkuPartNumber = $license.SkuPartNumber
ServicePlanId = $servicePlan.ServicePlanId
ServicePlanName = $servicePlan.ServicePlanName
ProvisioningStatus = $servicePlan.ProvisioningStatus
}
}
}
return $results
}
else {
# Display license information in the console
Write-Host "`n==== License Information for $($user.DisplayName) ($($user.UserPrincipalName)) ====`n" -ForegroundColor Cyan
if (-not $licenseDetails -or $licenseDetails.Count -eq 0) {
Write-Host "This user has no licenses assigned." -ForegroundColor Yellow
return
}
Write-Host "Assigned Licenses: $($licenseDetails.Count)" -ForegroundColor Green
foreach ($license in $licenseDetails) {
Write-Host "`n- SKU: $($license.SkuPartNumber)" -ForegroundColor Yellow
Write-Host " SKU ID: $($license.SkuId)"
Write-Host "`n Service Plans:" -ForegroundColor Green
foreach ($servicePlan in $license.ServicePlans) {
$status = Format-ServicePlanStatus -Status $servicePlan.ProvisioningStatus
$statusColor = if ($status -eq "Enabled") { "Green" } elseif ($status -eq "Disabled") { "Red" } else { "Yellow" }
Write-Host " - $($servicePlan.ServicePlanName) [$($servicePlan.ServicePlanId)]" -NoNewline
Write-Host " [$status]" -ForegroundColor $statusColor
}
}
}
}
catch {
Write-Error "An error occurred: $_"
Write-Error $_.Exception.StackTrace
}