-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-M365UserLicense.ps1
142 lines (139 loc) · 5.58 KB
/
Get-M365UserLicense.ps1
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
<#
.SYNOPSIS
This script will export user licenses from M365.
.DESCRIPTION
This script uses MSOL service. So make sure your PowerShell session is connected to MSOL service. `
This script will export list of users with their assigned M365 license. You have option to provide list of users from CSV file `
or use All switch to export license of all users in the tenant or use UserPrincipleName parameter to specify one or more users. `
If you use CSV file, the file must contain column heading as UserPrincipalName with no space and quotes.
.EXAMPLE
PS C:\> Get-M365UserLicenses.ps1 -All
This will export one or more license(s) of all users in the Microsoft 365 tenant.
.Example
PS C:\> Get-M365UserLicenses.ps1 -FileName Users.csv
This will export one or more license(s) of users listed in the CSV file. The file must contain column hader named UserPrincipalName with list of UPN of users.
.Example
PS C:\> Get-M365UserLicenses.ps1 -All -ExportCsv UserLicenseFile.csv
This will export list of users and their license of all users in the tenant to CSV file.
.Example
PS C:\> Get-M365UserLicenses.ps1 -FileName listofusers.csv -ExportCsv UserLicenseFile.csv
This will export license of only users present in Listofusers.csv file. Note that the file must have column header 'UserPrincipalName' with no space and quotes.
.Example
PS C:\> Get-M365UserLicenses.ps1 -UserPrincipalName username@domain.com
This will show license of one user.
.Example
PS C:\> Get-M365UserLicenses.ps1 -UserPrincipalName user1@domain.com, user2@domain.com
This will show license of more than one user.
.INPUTS
Inputs (if any)
.OUTPUTS
Output (if any)
.NOTES
General notes
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$false, Mandatory=$false)][Switch]$All,
[Parameter(ValueFromPipeline=$false, Mandatory=$false)][String]$FileName,
[Parameter(ValueFromPipeline=$false, Mandatory=$false)][String]$ExportCsv,
[Parameter(ValueFromPipeline=$false, Mandatory=$false)][String[]]$UserPrincipalName
)
#Parameter Validation
if (($FileName) -and ($All) ) {
Write-Host "Error: You cannot use -FileName and -All parameter at the same time. `nnPlease choose correct parameter name. `nSee script examples using help for more information." -ForegroundColor Red
break
}
elseif (($FileName) -and ($UserPrincipalName)) {
Write-Host "Error: You cannot use -FileName and -UserPrincipalName parameters at the same time. Please choose correct parameter name. See script examples using help for more information." -ForegroundColor Red
break
}
elseif (($All) -and ($UserPrincipalName) ) {
Write-Host "Error: You cannot use -All and -UserPrincipalName parameters at the same time. `nPlease choose correct parameter name. `nSee script examples using help for more information." -ForegroundColor Red
break
}
#function block
function Get-License {
#Declaring variables as Array.
$FinalReport = @()
$GroupObject = @()
$GetLicense = @()
#running ForEach statement
foreach ($User in $Users) {
$UPN = $User.UserPrincipalName
$UserAccount = Get-MsolUser -UserPrincipalName $UPN -ErrorAction SilentlyContinue
if ($UserAccount) {
$GetLicense = ($UserAccount).Licenses
for($i = 0; $i -lt $GetLicense.Count; $i++){
$LicArray += $GetLicense[$i].AccountSkuId + "; "
}
if ($LicArray -eq "") {
Write-Host "$($UPN) is unlicensed:" -ForegroundColor Cyan
$LicArray = "User Unlicensed"
}
else {
$LicArray = ($LicArray).TrimEnd('; ')
Write-Host "$($UPN) has following licenses:" -ForegroundColor Green
Write-Host "$LicArray"
}
#Write-Host "`n"
$ObjectProperties = [Ordered]@{
"UserPrincipalName" = $UPN
"License" = $LicArray
}
$GroupObject = New-Object -TypeName PSObject -Property $ObjectProperties
$FinalReport += $GroupObject
#declaring variable $licArray as null for next loop.
$LicArray = ""
}
else {
Write-Host "Error: User $UPN not found." -ForegroundColor Red
}
}
if ($ExportCsv) {
Write-Host "Exporting to csv..." -ForegroundColor Magenta
$FinalReport | Export-Csv "$ExportCsv" -NoTypeInformation
}
}
<# If statement to check paramenter inputs.
If FileName is used then user is processed based on list of users available in CSV file.
If All switch is used then all users will be retrieved.
If CSV file is used with FileName parameter then users in CSV file will be used.
#>
if ($FileName) {
try {
$Users = Import-Csv $FileName
}
catch {
$ErrorMessage = $_.Exception.Message
Write-Host "Error: $ErrorMessage" -ForegroundColor Red
exit
}
$Headers = ($Users | Get-Member -MemberType NoteProperty).Name
$UPNHeading = "UserPrincipalName"
if ($UPNHeading -in $Headers) {
#calling function
Get-License
}
else {
Write-Host "Error: CSV file doesn't contain header named UserPrincipalName" -ForegroundColor Red
exit
}
}
elseif ($All) {
#Retrieve all users in the tenant
$Users = Get-MsolUser -All
#calling function
Get-License
}
elseif ($UserPrincipalName) {
$Users = @()
foreach ($item in $UserPrincipalName) {
#Creating hashtable
$newUsersObj = [PSCustomObject] @{
'UserPrincipalName' = "$item"
}
$Users += $newUsersObj
}
#calling function
Get-License
}