get-O365LicenseOverview.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<#PSScriptInfo
.VERSION 1.0
.GUID ad23a25e-d924-42a1-b9bf-30e5ef65c170
.AUTHOR
 Maarten Peeters - Cloud Securitea - https://www.cloudsecuritea.com
.COMPANYNAME
 Cloud Securitea
.COPYRIGHT
.TAGS
 Subscriptions, licenses, office365
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
 MSOnline
.RELEASENOTES
 Version 1.0: Original published version.
#>
 

<#
.SYNOPSIS
 Quickly generate an Office 365 subscription and license overview
 
.DESCRIPTION
 This script will generate a HTML file to list the following:
 List of all subscriptions
 List of all SKU's combined
 List of all groups who have licenses connected to them
 List of all unlicensed users who have no role assigned
 List of all unlicensed users who have a role assigned
 List of all licensed users who have a role assigned
 List of all licensed users who get their license directly
 List of all licensed users who get their license via group assignement
 
.PARAMETER LogPath
 Enter the full path to store the HTML report of the Office 365 subscription and license overview
 For example: C:\Install
 
.EXAMPLE
 Get-O365LicenseOverview.ps1 -LogPath "C:\Install"
 
 .NOTES
 Version: 1.0
 Author: Maarten Peeters
 Creation Date: 04-09-2019
 Purpose/Change: Quickly generate an overview of Office 365 Subscriptions and licenses
#>


param(
    [Parameter(mandatory=$true)]
    [string] $LogPath
)

try{
    #Verify if MSOnline Module is available
    if (Get-Module -ListAvailable -Name MSonline) {
        #Import MSOnline Module
        import-module MSOnline -ErrorAction SilentlyContinue

        #Test if logpath exists
        If(Test-Path $LogPath) { 
            #Start script
            Try{
                #Object collections
                $subscriptionCollection = @()
                $accountSkuCollection = @()
                $licensedGroupCollection = @()
                $usersWithRolesCollection = @()
                $usersLicensedByGroupCollection = @()
                $usersLicensedDirectlyCollection = @()
                $usersUnlicensedNoRoleCollection = @()
                $usersUnlicensedWithRoleCollection = @()
                $usersLicensedWithRoleCollection = @()

                #Connect to the correct O365 Tenant
                Connect-MsolService

                #Retrieve all subscriptions and place this information in an object
                $subscriptions = Get-MsolSubscription
                foreach($subscription in $subscriptions){
                    $subscriptionCollection += new-object psobject -property @{ObjectId=$subscription.ObjectId;SkuPartNumber=$subscription.SkuPartNumber;Status=$subscription.Status;TotalLicenses=$subscription.TotalLicenses}
                }

                #Retrieve all account Sku's and place this information in an object
                $accountSkus = Get-MsolAccountSku
                foreach($accountSku in $accountSkus){
                    $accountSkuCollection += new-object psobject -property @{SkuPartNumber=$accountSku.SkuPartNumber;ActiveUnits=$accountSku.ActiveUnits;ConsumedUnits=$accountSku.ConsumedUnits}
                }

                #Retrieve group which have licenses assigned to them and place this information in an object
                $licensedGroups = Get-MsolGroup | Where-Object {$_.licenses -ne ""}
                foreach($licensedGroup in $licensedGroups){
                    $groupMembers = (Get-MsolGroupMember -GroupObjectId $licensedGroup.ObjectId).count
                    $licenses = $licensedGroup.licenses
                    foreach($license in $licenses){
                        $licensedGroupCollection += new-object psobject -property @{ObjectID=$licensedGroup.objectID;DisplayName=$licensedGroup.DisplayName;Members=$GroupMembers;license=$license.SkuPartNumber}
                    }
                }

                #Retrieve all users who have roles assigned
                $roles = Get-MsolRole
                foreach($role in $roles){
                    $members = Get-MsolRoleMember -RoleObjectId $role.ObjectId
                    foreach($member in $members){
                        $usersWithRolesCollection += new-object psobject -property @{DisplayName=$member.DisplayName;RoleName=$role.name}
                    }
                }

                #Retrieve all users and place them in their appropiate collection
                $users = Get-MsolUser | Sort-Object DisplayName

                foreach ($user in $users){
                    #check if synced account
                    if($user.ImmutableId -eq $null){
                        $synced = "False"
                    }
                    else{
                        $synced = "False"
                    }

                    #Unlicensed users
                    if ($user.isLicensed -eq $false){
                        #if the user has an admin role or not
                        $adminRole = "no"
                        foreach($userWithRoles in $usersWithRolesCollection){
                            if($user.DisplayName -eq $userWithRoles.DisplayName){
                                $adminRole = "yes"
                            }
                        }
                        if($adminRole -eq "no"){
                            $usersUnlicensedNoRoleCollection += new-object psobject -property @{DisplayName=$user.DisplayName;UserType=$user.UserType;UPN=$user.userprincipalname;License=$user.isLicensed;Admin="False";BlockCredential=$user.BlockCredential;Synced=$Synced}
                        }
                        else{
                            $usersUnlicensedWithRoleCollection += new-object psobject -property @{DisplayName=$user.DisplayName;UserType=$user.UserType;UPN=$user.userprincipalname;License=$user.isLicensed;Admin="True";BlockCredential=$user.BlockCredential;Synced=$Synced}
                        }
                    }

                    #Licensed with admin role
                    if ($user.isLicensed -eq $true){
                        #if the user has an admin role or not
                        $adminRole = "no"
                        foreach($userWithRoles in $usersWithRolesCollection){
                            if($user.DisplayName -eq $userWithRoles.DisplayName){
                                $adminRole = "yes"
                            }
                        }
                        if($adminRole -eq "yes"){
                            $usersLicensedWithRoleCollection += new-object psobject -property @{DisplayName=$user.DisplayName;UserType=$user.UserType;UPN=$user.userprincipalname;License=$user.isLicensed;Admin="True";BlockCredential=$user.BlockCredential;Synced=$Synced}
                        }
                    }

                    #Licensed users and verify if the license is coming from a group or directly
                    if ($user.isLicensed -eq $true){
                        $licenses = $user.licenses
                        
                        foreach($license in $licenses){
                            #You can use Groups Assigning License but I noticed that if it's not assigned via a group it will be the users GUID. I will verify the group with the earlier collection.
                            $groupAssignedFound = "no"
                            $groupAssigned = ""
                            foreach($groupAssigningLicense in $license.GroupsAssigningLicense){
                                $groupAssigned = $licensedGroupCollection | where-object {$_.ObjectID -eq $groupAssigningLicense.GUID}
                                if($groupAssigned){
                                    $groupAssignedFound = "yes"
                                }
                            }
                            
                            if ($groupAssignedFound -eq "no"){
                                $usersLicensedDirectlyCollection += new-object psobject -property @{DisplayName=$user.DisplayName;UserType=$user.UserType;UPN=$user.userprincipalname;License=$license.accountsku.skupartnumber;BlockCredential=$user.BlockCredential;Synced=$Synced}
                            }
                            else{
                                $usersLicensedByGroupCollection += new-object psobject -property @{DisplayName=$user.DisplayName;UserType=$user.UserType;UPN=$user.userprincipalname;License=$license.accountsku.skupartnumber;BlockCredential=$user.BlockCredential;Synced=$Synced}
                            }
                        }
                    }
                }
                
                #We now have our collections so we are building the HTML page to get a direct view
                #List of all subscriptions
                $article = "<h2>List of all subscriptions</h2>"
                $article += "<table>
                            <tr>
                                <th>ObjectId</th>
                                <th>SkuPartNumber</th>
                                <th>Status</th>
                                <th>TotalLicenses</th>
                            </tr>"

                
                foreach($subscription in $subscriptionCollection){
                $article += "<tr>
                                <td>$($subscription.ObjectId)</td>
                                <td>$($subscription.SkuPartNumber)</td>
                                <td>$($subscription.Status)</td>
                                <td>$($subscription.TotalLicenses)</td>
                            </tr>"

                }
                
                $article += "</table>"
                
                #List of all SKU's combined
                $article += "<h2>List of all SKU's combined</h2>"
                $article += "<table>
                            <tr>
                                <th>SkuPartNumber</th>
                                <th>ActiveUnits</th>
                                <th>ConsumedUnits</th>
                            </tr>"

                
                foreach($accountSku in $accountSkuCollection){
                $article += "<tr>
                                <td>$($accountSku.SkuPartNumber)</td>
                                <td>$($accountSku.ActiveUnits)</td>
                                <td>$($accountSku.ConsumedUnits)</td>
                            </tr>"

                }
                
                $article += "</table>"
                
                #List of all groups who have licenses connected to them
                $article += "<h2>List of all groups who have licenses connected to them</h2>"
                $article += "<table>
                            <tr>
                                <th>ObjectID</th>
                                <th>DisplayName</th>
                                <th>Members</th>
                                <th>license</th>
                            </tr>"

                
                foreach($licensedGroup in $licensedGroupCollection){
                $article += "<tr>
                                <td>$($licensedGroup.ObjectID)</td>
                                <td>$($licensedGroup.DisplayName)</td>
                                <td>$($licensedGroup.Members)</td>
                                <td>$($licensedGroup.license)</td>
                            </tr>"

                }
                
                $article += "</table>"
                
                #List of all unlicensed users who have no role assigned
                $article += "<h2>List of all unlicensed users who have no role assigned</h2>"
                $article += "<table>
                            <tr>
                                <th>DisplayName</th>
                                <th>UserType</th>
                                <th>UPN</th>
                                <th>license</th>
                                <th>Admin</th>
                                <th>BlockCredential</th>
                                <th>Synced</th>
                            </tr>"

                
                foreach($usersUnlicensedNoRole in $usersUnlicensedNoRoleCollection){
                $article += "<tr>
                                <td>$($usersUnlicensedNoRole.DisplayName)</td>
                                <td>$($usersUnlicensedNoRole.UserType)</td>
                                <td>$($usersUnlicensedNoRole.UPN)</td>
                                <td>$($usersUnlicensedNoRole.license)</td>
                                <td>$($usersUnlicensedNoRole.Admin)</td>
                                <td>$($usersUnlicensedNoRole.BlockCredential)</td>
                                <td>$($usersUnlicensedNoRole.Synced)</td>
                            </tr>"

                }
                
                $article += "</table>"
                
                #List of all unlicensed users who have a role assigned
                $article += "<h2>List of all unlicensed users who have a role assigned</h2>"
                $article += "<table>
                            <tr>
                                <th>DisplayName</th>
                                <th>UserType</th>
                                <th>UPN</th>
                                <th>license</th>
                                <th>Admin</th>
                                <th>BlockCredential</th>
                                <th>Synced</th>
                            </tr>"

                
                foreach($usersUnlicensedWithRole in $usersUnlicensedWithRoleCollection){
                $article += "<tr>
                                <td>$($usersUnlicensedWithRole.DisplayName)</td>
                                <td>$($usersUnlicensedWithRole.UserType)</td>
                                <td>$($usersUnlicensedWithRole.UPN)</td>
                                <td>$($usersUnlicensedWithRole.license)</td>
                                <td>$($usersUnlicensedWithRole.Admin)</td>
                                <td>$($usersUnlicensedWithRole.BlockCredential)</td>
                                <td>$($usersUnlicensedWithRole.Synced)</td>
                            </tr>"

                }
                
                $article += "</table>"
                
                #List of all licensed users who have a role assigned
                $article += "<h2>List of all licensed users who have a role assigned</h2>"
                $article += "<table>
                            <tr>
                                <th>DisplayName</th>
                                <th>UserType</th>
                                <th>UPN</th>
                                <th>license</th>
                                <th>Admin</th>
                                <th>BlockCredential</th>
                                <th>Synced</th>
                            </tr>"

                
                foreach($usersLicensedWithRole in $usersLicensedWithRoleCollection){
                $article += "<tr>
                                <td>$($usersLicensedWithRole.DisplayName)</td>
                                <td>$($usersLicensedWithRole.UserType)</td>
                                <td>$($usersLicensedWithRole.UPN)</td>
                                <td>$($usersLicensedWithRole.license)</td>
                                <td>$($usersLicensedWithRole.Admin)</td>
                                <td>$($usersLicensedWithRole.BlockCredential)</td>
                                <td>$($usersLicensedWithRole.Synced)</td>
                            </tr>"

                }
                
                $article += "</table>"
                
                #List of all licensed users who get their license directly
                $article += "<h2>List of all licensed users who get their license directly</h2>"
                $article += "<table>
                            <tr>
                                <th>DisplayName</th>
                                <th>UserType</th>
                                <th>UPN</th>
                                <th>license</th>
                                <th>BlockCredential</th>
                                <th>Synced</th>
                            </tr>"

                
                foreach($usersLicensedDirectly in $usersLicensedDirectlyCollection){
                $article += "<tr>
                                <td>$($usersLicensedDirectly.DisplayName)</td>
                                <td>$($usersLicensedDirectly.UserType)</td>
                                <td>$($usersLicensedDirectly.UPN)</td>
                                <td>$($usersLicensedDirectly.license)</td>
                                <td>$($usersLicensedDirectly.BlockCredential)</td>
                                <td>$($usersLicensedDirectly.Synced)</td>
                            </tr>"

                }
                
                $article += "</table>"
                
                #List of all licensed users who get their license via group assignement
                $article += "<h2>List of all licensed users who get their license via group assignement</h2>"
                $article += "<table>
                            <tr>
                                <th>DisplayName</th>
                                <th>UserType</th>
                                <th>UPN</th>
                                <th>license</th>
                                <th>BlockCredential</th>
                                <th>Synced</th>
                            </tr>"

                
                foreach($usersLicensedByGroup in $usersLicensedByGroupCollection){
                $article += "<tr>
                                <td>$($usersLicensedByGroup.DisplayName)</td>
                                <td>$($usersLicensedByGroup.UserType)</td>
                                <td>$($usersLicensedByGroup.UPN)</td>
                                <td>$($usersLicensedByGroup.license)</td>
                                <td>$($usersLicensedByGroup.BlockCredential)</td>
                                <td>$($usersLicensedByGroup.Synced)</td>
                            </tr>"

                }
                
                $article += "</table>"
                
                $date = get-date
                $today = $date.ToString("ddMMyyyy_HHmm")
                $LogPath = Join-Path $LogPath "HTMLSubscriptionReport_$($today).html"    
                
                #Head
                $head = "
                    <html xmlns=`"http://www.w3.org/1999/xhtml`">
                        <head>
                            <style>
                                @charset `"UTF-8`";
 
                                @media print {
                                    body {-webkit-print-color-adjust: exact;}
                                }
                     
                                div.container {
                                    width: 100%;
                                    border: 1px solid gray;
                                }
                                 
                                header {
                                    padding: 0.1em;
                                    color: white;
                                    background-color: #000033;
                                    color: white;
                                    clear: left;
                                    text-align: center;
                                    border-bottom: 2px solid #FF0066
                                }    
                                 
                                footer {
                                    padding: 0.1em;
                                    color: white;
                                    background-color: #000033;
                                    color: white;
                                    clear: left;
                                    text-align: center;
                                    border-top: 2px solid #FF0066
                                }    
 
                                article {
                                    margin-left: 20px;
                                    min-width:600px;
                                    min-height: 600px;
                                    padding: 1em;
                                }
                                 
                                th{
                                    border:1px Solid Black;
                                    border-Collapse:collapse;
                                    background-color:#000033;
                                    color:white;
                                }
                                 
                                th{
                                    border:1px Solid Black;
                                    border-Collapse:collapse;
                                }
 
                            </style>
                        </head>    
                    "

                    
                    #Header
                    $date = (get-date).tostring("dd-MM-yyyy")
                    $header = "
                        <h1>Subscriptions Report</h1>
                        <h5>$($date)</h5>
                    "

                    
                    #Footer
                    $Footer = "
                        Copyright &copy;
                    "

                    
                    #Full HTML
                    $HTML = "
                        $($Head)        
                        <body class=`"Inventory`">
                            <div class=`"container`">
                                <header>
                                    $($Header)
                                </header>
                                 
                                <article>
                                    $($article)
                                </article>
                                         
                                <footer>
                                    $($footer)
                                </footer>
                            </div>    
                        </body>
                        </html>
                    "
 
                    add-content $HTML -path $LogPath

                    Write-Host "Office 365 subscription and license overview created at $($LogPath), it will also open automatically in 5 seconds" -foregroundcolor green

                    start-sleep -s 5
                    Invoke-Item $LogPath
            }
            catch{
                write-host "Error occurred: $($_.Exception.Message), please post this error on https://www.cloudsecuritea.com" -foregroundcolor red
            }
        } Else { 
            Write-Host "The path $($LogPath) could not be found. Please enter a correct path to store the Office 365 subscription and license overview" -foregroundcolor yellow
        }
    } else {
        Write-Host "MSOnline module not loaded. Please install the MSOnline module with Install-Module MSOnline" -foregroundcolor yellow
    }
}
catch{
    write-host "Error occurred: $($_.Exception.Message)" -foregroundcolor red
}