-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-TenantSecureScore.ps1
More file actions
1307 lines (1127 loc) · 54.8 KB
/
Get-TenantSecureScore.ps1
File metadata and controls
1307 lines (1127 loc) · 54.8 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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Retrieves and generates comprehensive Microsoft 365 Secure Score reports for single or multiple tenants.
.DESCRIPTION
Get-TenantSecureScore.ps1 connects to Microsoft Graph API to retrieve Secure Score data,
control profiles, and historical trends for Microsoft 365 tenants. It generates detailed
HTML reports with actionable recommendations and CSV exports for further analysis.
The script can process:
- Single tenant using direct credential parameters
- Multiple tenants using a CSV file with credentials
- Filtered client from CSV using -ClientName parameter
Generated reports include:
- Visual dashboard with key metrics
- Quick wins (low effort, high impact improvements)
- High priority recommendations based on Microsoft rankings
- Detailed control analysis with remediation steps
- Category summaries and trend analysis
- Consolidated multi-tenant overview (when using CSV)
.PARAMETER TenantId
The Azure AD Tenant ID (GUID format).
Required when not using -CsvPath.
Example: "12345678-1234-1234-1234-123456789012"
.PARAMETER ClientId
The Azure AD Application (Client) ID with Microsoft Graph permissions.
Required when not using -CsvPath.
Required API Permissions:
- SecurityEvents.Read.All
- Organization.Read.All
- Domain.Read.All
Example: "87654321-4321-4321-4321-210987654321"
.PARAMETER ClientSecret
The client secret (application password) for the Azure AD application.
Required when not using -CsvPath.
Note: Store secrets securely. Consider using Azure Key Vault or secure credential storage.
.PARAMETER CsvPath
Path to a CSV file containing credentials for multiple tenants.
When specified, processes all tenants in the CSV and generates a consolidated report.
Required CSV columns:
- Client: Friendly name for the tenant
- Tenant ID: Azure AD Tenant ID
- Client ID: Application ID
- Key Value: Client Secret
Example: "C:\Credentials\Tenants.csv"
.PARAMETER ClientName
When using -CsvPath, filter to process only the specified client.
Must exactly match a value in the "Client" column of the CSV.
Example: "Contoso Ltd"
.PARAMETER OutputPath
Custom output directory path for generated reports.
If not specified, creates a timestamped folder: "SecureScore-Report-YYYYMMDD-HHMMSS"
Example: "C:\Reports\SecureScore"
.OUTPUTS
For each tenant processed:
- HTML Report: Comprehensive visual report with recommendations
- CSV Export: Detailed control data for analysis
When processing multiple tenants:
- Consolidated HTML: Multi-tenant overview dashboard
- Summary CSV: Aggregated scores and metrics
.EXAMPLE
.\Get-TenantSecureScore.ps1 -TenantId "12345678-90ab-cdef-1234-567890abcdef" `
-ClientId "abcdef12-3456-7890-abcd-ef1234567890" `
-ClientSecret "your-secret-here"
Processes a single tenant and generates HTML and CSV reports in a timestamped folder.
.EXAMPLE
.\Get-TenantSecureScore.ps1 -TenantId $tid -ClientId $cid -ClientSecret $secret `
-OutputPath "C:\Reports\SecureScore\Contoso"
Processes a single tenant with output saved to a custom directory.
.EXAMPLE
.\Get-TenantSecureScore.ps1 -CsvPath "C:\Credentials\Tenants.csv"
Processes all tenants listed in the CSV file and generates individual reports
plus a consolidated multi-tenant dashboard.
.EXAMPLE
.\Get-TenantSecureScore.ps1 -CsvPath "C:\Credentials\Tenants.csv" `
-ClientName "Contoso Ltd"
Processes only the "Contoso Ltd" tenant from the CSV file.
.EXAMPLE
.\Get-TenantSecureScore.ps1 -CsvPath ".\tenants.csv" -OutputPath "C:\SecureScore\Reports"
Processes all tenants from CSV with output to a custom directory.
.NOTES
File Name : Get-TenantSecureScore.ps1
Author : Geoff Tankersley
Prerequisite : PowerShell 5.1 or higher
Version : 2.0
Requirements:
- Azure AD App Registration with appropriate permissions
- Microsoft Graph API access
- Internet connectivity to Microsoft Graph endpoints
API Permissions Required:
- SecurityEvents.Read.All (Application)
- Organization.Read.All (Application)
- Domain.Read.All (Application)
CSV File Format:
Create a CSV file with these exact column headers:
Client,Tenant ID,Client ID,Key Value
"Contoso Ltd","tenant-guid-here","app-guid-here","secret-here"
"Fabrikam Inc","tenant-guid-here","app-guid-here","secret-here"
#>
param(
[Parameter(Mandatory=$false)]
[string]$TenantId,
[Parameter(Mandatory=$false)]
[string]$ClientId,
[Parameter(Mandatory=$false)]
[string]$ClientSecret,
[Parameter(Mandatory=$false)]
[string]$CsvPath,
[Parameter(Mandatory=$false)]
[string]$ClientName,
[Parameter(Mandatory=$false)]
[string]$OutputPath
)
function Get-MsGraphToken {
param (
[Parameter(Mandatory=$true)]
[string]$TenantId,
[Parameter(Mandatory=$true)]
[string]$ClientId,
[Parameter(Mandatory=$true)]
[string]$ClientSecret
)
$tokenUrl = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$body = @{
client_id = $ClientId
client_secret = $ClientSecret
scope = "https://graph.microsoft.com/.default"
grant_type = "client_credentials"
}
try {
$response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $body -ContentType "application/x-www-form-urlencoded"
return $response.access_token
}
catch {
Write-Error "Error obtaining access token: $($_.Exception.Message)"
throw $_
}
}
function Get-TenantBasicInfo {
param (
[Parameter(Mandatory=$true)]
[string]$AccessToken,
[Parameter(Mandatory=$false)]
[string]$TenantId
)
$headers = @{
"Authorization" = "Bearer $AccessToken"
"Content-Type" = "application/json"
}
try {
$domainUri = "https://graph.microsoft.com/v1.0/domains"
$domainsResponse = Invoke-RestMethod -Uri $domainUri -Method Get -Headers $headers
$initialDomain = ($domainsResponse.value | Where-Object { $_.isInitial -eq $true }).id
if (-not $initialDomain) {
$initialDomain = "unknown.onmicrosoft.com"
}
$displayName = "Unknown"
try {
$orgUri = "https://graph.microsoft.com/v1.0/organization"
$orgResponse = Invoke-RestMethod -Uri $orgUri -Method Get -Headers $headers
if ($orgResponse.value -and $orgResponse.value.Count -gt 0) {
$displayName = $orgResponse.value[0].displayName
}
}
catch {
if ($TenantId) {
$displayName = "Tenant $TenantId"
}
}
return [PSCustomObject]@{
TenantId = $TenantId
DisplayName = $displayName
InitialDomain = $initialDomain
}
}
catch {
Write-Warning "Error retrieving tenant information: $($_.Exception.Message)"
return [PSCustomObject]@{
TenantId = $TenantId
DisplayName = if ($TenantId) { "Tenant $TenantId" } else { "Unknown" }
InitialDomain = "unknown.onmicrosoft.com"
}
}
}
function Get-SecureScore {
param (
[Parameter(Mandatory=$true)]
[string]$AccessToken
)
$headers = @{
"Authorization" = "Bearer $AccessToken"
"Content-Type" = "application/json"
}
try {
$scoreUri = "https://graph.microsoft.com/v1.0/security/secureScores?`$top=1"
Write-Host " Calling: $scoreUri" -ForegroundColor Gray
$scoreResponse = Invoke-RestMethod -Uri $scoreUri -Method Get -Headers $headers
if ($scoreResponse.value.Count -eq 0) {
Write-Warning "No secure score data available"
return $null
}
return $scoreResponse.value[0]
}
catch {
Write-Error "Error retrieving secure score: $($_.Exception.Message)"
return $null
}
}
function Get-SecureScoreHistory {
param (
[Parameter(Mandatory=$true)]
[string]$AccessToken,
[Parameter(Mandatory=$false)]
[int]$Months = 3
)
$headers = @{
"Authorization" = "Bearer $AccessToken"
"Content-Type" = "application/json"
}
try {
$startDate = (Get-Date).AddMonths(-$Months).ToString("yyyy-MM-dd")
$historyUri = "https://graph.microsoft.com/v1.0/security/secureScores?`$filter=createdDateTime ge $startDate&`$orderby=createdDateTime"
Write-Host " Calling: $historyUri" -ForegroundColor Gray
$historyResponse = Invoke-RestMethod -Uri $historyUri -Method Get -Headers $headers
return $historyResponse.value
}
catch {
Write-Warning "Error retrieving secure score history: $($_.Exception.Message)"
return @()
}
}
function Get-SecureScoreControlProfiles {
param (
[Parameter(Mandatory=$true)]
[string]$AccessToken
)
$headers = @{
"Authorization" = "Bearer $AccessToken"
"Content-Type" = "application/json"
}
try {
$controlProfilesUri = "https://graph.microsoft.com/v1.0/security/secureScoreControlProfiles"
Write-Host "Retrieving secure score control profiles..." -ForegroundColor Yellow
Write-Host " Calling: $controlProfilesUri" -ForegroundColor Gray
$allControlProfiles = @()
$nextLink = $controlProfilesUri
# Handle pagination
do {
$response = Invoke-RestMethod -Uri $nextLink -Method Get -Headers $headers
$allControlProfiles += $response.value
$nextLink = $response.'@odata.nextLink'
if ($nextLink) {
Write-Host " Retrieved $($allControlProfiles.Count) control profiles, fetching more..." -ForegroundColor Gray
}
} while ($nextLink)
Write-Host " Total control profiles retrieved: $($allControlProfiles.Count)" -ForegroundColor Green
return $allControlProfiles
}
catch {
Write-Error "Error retrieving secure score control profiles: $($_.Exception.Message)"
return @()
}
}
function Merge-ControlData {
param (
[Parameter(Mandatory=$true)]
[object]$SecureScore,
[Parameter(Mandatory=$true)]
[array]$ControlProfiles
)
Write-Host "Merging control scores with control profiles..." -ForegroundColor Yellow
# Create lookup table for control profiles
$profileLookup = @{}
foreach ($profile in $ControlProfiles) {
$profileLookup[$profile.id] = $profile
}
Write-Host " Profile lookup table created with $($profileLookup.Count) entries" -ForegroundColor Gray
$mergedControls = @()
$matchedCount = 0
$unmatchedCount = 0
# Process each control score from the secure score API
foreach ($controlScore in $SecureScore.controlScores) {
$controlId = $controlScore.controlName
$profile = $profileLookup[$controlId]
# Get values from controlScore - use actual properties that exist
$currentScore = if ($controlScore.score -ne $null) { [double]$controlScore.score } else { 0 }
$scoreInPercentage = if ($controlScore.scoreInPercentage -ne $null) { [double]$controlScore.scoreInPercentage } else { 0 }
# Try to get implementation status from controlScore first
$implementationStatus = "Unknown"
if ($controlScore.implementationStatus) {
# Parse the implementation status string
$statusText = $controlScore.implementationStatus
if ($statusText -match "On|Implemented|Completed") {
$implementationStatus = "Implemented"
}
elseif ($statusText -match "Off|Not") {
$implementationStatus = "Not Implemented"
}
else {
$implementationStatus = "Partial"
}
}
if ($profile) {
$matchedCount++
# Get maxScore from profile
$maxScore = if ($profile.maxScore) { [double]$profile.maxScore } else { 0 }
# Calculate percentage if not provided or to verify
if ($maxScore -gt 0 -and $scoreInPercentage -eq 0) {
$calculatedPercentage = [math]::Round(($currentScore / $maxScore) * 100, 2)
}
else {
$calculatedPercentage = $scoreInPercentage
}
# Determine implementation status from percentage if needed
if ($implementationStatus -eq "Unknown") {
if ($calculatedPercentage -eq 100) {
$implementationStatus = "Implemented"
}
elseif ($calculatedPercentage -eq 0) {
$implementationStatus = "Not Implemented"
}
else {
$implementationStatus = "Partial"
}
}
$mergedControl = [PSCustomObject]@{
# Basic identification
Id = $controlId
Title = $profile.title
Description = if ($profile.remediation) { $profile.remediation } else { $controlScore.description }
# Scoring - from BOTH APIs
MaxScore = $maxScore
CurrentScore = $currentScore
PercentageComplete = $calculatedPercentage
ImplementationStatus = $implementationStatus
# Categorization
ControlCategory = if ($profile.controlCategory) { $profile.controlCategory } else { $controlScore.controlCategory }
Service = $profile.service
# Detailed recommendation fields from control profile
Remediation = $profile.remediation
RemediationImpact = $profile.remediationImpact
ImplementationCost = if ($profile.implementationCost -and $profile.implementationCost -ne "Unknown") { $profile.implementationCost } else { "Not Specified" }
UserImpact = if ($profile.userImpact -and $profile.userImpact -ne "Unknown") { $profile.userImpact } else { "Not Specified" }
ActionType = $profile.actionType
ActionUrl = $profile.actionUrl
Rank = if ($profile.rank) { $profile.rank } else { 999 }
Threats = if ($profile.threats) { ($profile.threats -join "; ") } else { "" }
Tier = $profile.tier
Deprecated = $profile.deprecated
# Compliance info
ComplianceFrameworks = if ($profile.complianceInformation) {
($profile.complianceInformation | ForEach-Object {
"$($_.certificationName):$($_.certificationControls -join ',')"
}) -join "; "
} else { "" }
# Additional metadata
LastModified = $profile.lastModifiedDateTime
LastSynced = $controlScore.lastSynced
VendorInfo = if ($profile.vendorInformation) {
"$($profile.vendorInformation.provider) - $($profile.vendorInformation.vendor)"
} else { "Microsoft" }
}
$mergedControls += $mergedControl
}
else {
$unmatchedCount++
Write-Verbose "No profile found for control: $controlId"
# Control score exists but no profile - use what we have from controlScore
# Calculate implementation status from percentage if available
if ($implementationStatus -eq "Unknown" -and $scoreInPercentage -gt 0) {
if ($scoreInPercentage -eq 100) {
$implementationStatus = "Implemented"
}
elseif ($scoreInPercentage -eq 0) {
$implementationStatus = "Not Implemented"
}
else {
$implementationStatus = "Partial"
}
}
$mergedControl = [PSCustomObject]@{
Id = $controlId
Title = $controlId
Description = $controlScore.description
MaxScore = 0
CurrentScore = $currentScore
PercentageComplete = $scoreInPercentage
ImplementationStatus = $implementationStatus
ControlCategory = $controlScore.controlCategory
Service = "Unknown"
Remediation = $controlScore.description
RemediationImpact = ""
ImplementationCost = "Not Specified"
UserImpact = "Not Specified"
ActionType = ""
ActionUrl = ""
Rank = 999
Threats = ""
Tier = ""
Deprecated = $false
ComplianceFrameworks = ""
LastModified = ""
LastSynced = $controlScore.lastSynced
VendorInfo = "Microsoft"
}
$mergedControls += $mergedControl
}
}
Write-Host " Matched $matchedCount controls with profiles, $unmatchedCount without profiles" -ForegroundColor $(if ($unmatchedCount -gt 0) { "Yellow" } else { "Green" })
Write-Host " Total merged controls: $($mergedControls.Count)" -ForegroundColor Green
return $mergedControls
}
function Format-SecureScoreReport {
param (
[Parameter(Mandatory=$true)]
[object]$SecureScore,
[Parameter(Mandatory=$true)]
[array]$ControlDetails,
[Parameter(Mandatory=$false)]
[array]$ScoreHistory = @()
)
# Calculate metrics
$averageScore = if ($ScoreHistory.Count -gt 0) {
[math]::Round(($ScoreHistory | Measure-Object -Property currentScore -Average).Average, 2)
} else { 0 }
$maxPossibleScore = if ($SecureScore.maxScore) { $SecureScore.maxScore } else { 0 }
$currentScore = if ($SecureScore.currentScore) { $SecureScore.currentScore } else { 0 }
$percentComplete = if ($maxPossibleScore -gt 0) { [math]::Round(($currentScore / $maxPossibleScore) * 100, 2) } else { 0 }
# Count implementation statuses
$implementedControls = ($ControlDetails | Where-Object { $_.ImplementationStatus -eq "Implemented" }).Count
$notImplementedControls = ($ControlDetails | Where-Object { $_.ImplementationStatus -eq "Not Implemented" }).Count
$partialControls = ($ControlDetails | Where-Object { $_.ImplementationStatus -eq "Partial" }).Count
$unknownControls = ($ControlDetails | Where-Object { $_.ImplementationStatus -eq "Unknown" }).Count
# Group controls by category
$controlsByCategory = if ($ControlDetails.Count -gt 0) {
$ControlDetails | Group-Object -Property ControlCategory
} else {
@()
}
# Top improvement opportunities - not implemented or partial, has maxScore, sorted by max score
$improvementOpportunities = $ControlDetails |
Where-Object {
$_.ImplementationStatus -ne "Implemented" -and
-not $_.Deprecated -and
$_.MaxScore -gt 0
} |
Sort-Object -Property @{Expression="MaxScore"; Descending=$true}, @{Expression="Rank"; Descending=$false} |
Select-Object -First 25
# Quick wins - high impact, low/moderate cost, not implemented
$quickWins = $ControlDetails |
Where-Object {
$_.ImplementationStatus -ne "Implemented" -and
-not $_.Deprecated -and
$_.MaxScore -gt 0 -and
($_.ImplementationCost -eq "Low" -or $_.ImplementationCost -eq "Moderate")
} |
Sort-Object -Property @{Expression="MaxScore"; Descending=$true} |
Select-Object -First 15
# High priority - high rank (low rank number = high priority), high max score
$highPriority = $ControlDetails |
Where-Object {
$_.ImplementationStatus -ne "Implemented" -and
-not $_.Deprecated -and
$_.MaxScore -gt 5 -and
$_.Rank -gt 0 -and
$_.Rank -le 30
} |
Sort-Object -Property Rank |
Select-Object -First 15
$reportInfo = [PSCustomObject]@{
CurrentScore = $currentScore
MaxPossibleScore = $maxPossibleScore
PercentComplete = $percentComplete
AverageScore = $averageScore
ScoreTrend = if ($ScoreHistory.Count -gt 1) {
$firstScore = $ScoreHistory[0].currentScore
$lastScore = $ScoreHistory[-1].currentScore
[math]::Round(($lastScore - $firstScore), 2)
} else { 0 }
ImplementedControls = $implementedControls
NotImplementedControls = $notImplementedControls
PartialControls = $partialControls
UnknownControls = $unknownControls
ControlsByCategory = $controlsByCategory
ImprovementOpportunities = $improvementOpportunities
QuickWins = $quickWins
HighPriority = $highPriority
LastUpdated = if ($SecureScore.createdDateTime) { $SecureScore.createdDateTime } else { (Get-Date).ToString() }
AllControls = $ControlDetails
}
Write-Host "`nReport Summary:" -ForegroundColor Cyan
Write-Host " Current Score: $currentScore / $maxPossibleScore ($percentComplete%)" -ForegroundColor White
Write-Host " Implemented: $implementedControls | Not Implemented: $notImplementedControls | Partial: $partialControls | Unknown: $unknownControls" -ForegroundColor White
Write-Host " Total Controls: $($ControlDetails.Count)" -ForegroundColor White
Write-Host " Improvement Opportunities: $($improvementOpportunities.Count)" -ForegroundColor Yellow
Write-Host " Quick Wins Available: $($quickWins.Count)" -ForegroundColor Green
Write-Host " High Priority Items: $($highPriority.Count)" -ForegroundColor Magenta
return $reportInfo
}
function Get-SecureScoreHtml {
param (
[Parameter(Mandatory=$true)]
[string]$TenantName,
[Parameter(Mandatory=$true)]
[string]$TenantDomain,
[Parameter(Mandatory=$true)]
[object]$ReportInfo
)
# Helper function to escape HTML and remove HTML tags from remediation
function ConvertTo-HtmlSafe {
param([string]$Text)
if ([string]::IsNullOrEmpty($Text)) { return "" }
return [System.Net.WebUtility]::HtmlEncode($Text)
}
function Strip-HtmlTags {
param([string]$Html)
if ([string]::IsNullOrEmpty($Html)) { return "" }
# Remove HTML tags but keep line breaks
$text = $Html -replace '<br\s*/?>',"`n" -replace '<[^>]+>','' -replace ' ',' '
return $text.Trim()
}
# Generate Quick Wins section
$quickWinsHtml = ""
if ($ReportInfo.QuickWins.Count -gt 0) {
$quickWinsRows = ($ReportInfo.QuickWins | ForEach-Object {
$safeTitle = ConvertTo-HtmlSafe $_.Title
$cleanRemediation = Strip-HtmlTags $_.Remediation
$safeRemediation = ConvertTo-HtmlSafe $cleanRemediation
$actionUrl = if ($_.ActionUrl) { ConvertTo-HtmlSafe $_.ActionUrl } else { "#" }
$costClass = switch ($_.ImplementationCost) {
"Low" { "success" }
"Moderate" { "warning" }
"High" { "failure" }
default { "" }
}
@"
<tr>
<td><strong>$safeTitle</strong><br><small style="color: #666;">$($safeRemediation.Substring(0, [Math]::Min(300, $safeRemediation.Length)))$(if ($safeRemediation.Length -gt 300) { "..." })</small></td>
<td style="text-align: center; font-weight: bold;">$($_.MaxScore)</td>
<td style="text-align: center;" class="$costClass">$($_.ImplementationCost)</td>
<td style="text-align: center;">$($_.ControlCategory)</td>
<td style="text-align: center;"><a href="$actionUrl" target="_blank" style="padding: 4px 12px; background: #0078D4; color: white; text-decoration: none; border-radius: 4px; font-size: 0.85em;">Configure</a></td>
</tr>
"@
}) -join ""
$quickWinsHtml = @"
<div class="card">
<h2>⚡ Quick Wins (Low-Moderate Cost, High Impact)</h2>
<p style="color: #666;">These controls offer significant security improvements with reasonable effort. Start here for maximum ROI.</p>
<div class="table-container">
<table>
<tr>
<th>Control & Remediation Steps</th>
<th style="text-align: center; width: 80px;">Points</th>
<th style="text-align: center; width: 120px;">Cost</th>
<th style="text-align: center; width: 100px;">Category</th>
<th style="text-align: center; width: 100px;">Action</th>
</tr>
$quickWinsRows
</table>
</div>
</div>
"@
}
# Generate High Priority section
$highPriorityHtml = ""
if ($ReportInfo.HighPriority.Count -gt 0) {
$highPriorityRows = ($ReportInfo.HighPriority | ForEach-Object {
$safeTitle = ConvertTo-HtmlSafe $_.Title
$cleanRemediation = Strip-HtmlTags $_.Remediation
$safeRemediation = ConvertTo-HtmlSafe $cleanRemediation
$safeThreats = ConvertTo-HtmlSafe $_.Threats
$actionUrl = if ($_.ActionUrl) { ConvertTo-HtmlSafe $_.ActionUrl } else { "#" }
@"
<tr>
<td style="text-align: center; font-weight: bold;">$($_.Rank)</td>
<td><strong>$safeTitle</strong><br><small style="color: #666;">$($safeRemediation.Substring(0, [Math]::Min(300, $safeRemediation.Length)))$(if ($safeRemediation.Length -gt 300) { "..." })</small></td>
<td style="text-align: center; font-weight: bold;">$($_.MaxScore)</td>
<td><small style="color: #666;">$safeThreats</small></td>
<td style="text-align: center;">$($_.ImplementationCost)</td>
<td style="text-align: center;"><a href="$actionUrl" target="_blank" style="padding: 4px 12px; background: #0078D4; color: white; text-decoration: none; border-radius: 4px; font-size: 0.85em;">Configure</a></td>
</tr>
"@
}) -join ""
$highPriorityHtml = @"
<div class="card">
<h2>🎯 High Priority Recommendations</h2>
<p style="color: #666;">Microsoft's top-ranked security controls based on threat intelligence and security best practices.</p>
<div class="table-container">
<table>
<tr>
<th style="text-align: center; width: 60px;">Rank</th>
<th>Control & Remediation Steps</th>
<th style="text-align: center; width: 80px;">Points</th>
<th style="width: 200px;">Threats Mitigated</th>
<th style="text-align: center; width: 120px;">Cost</th>
<th style="text-align: center; width: 100px;">Action</th>
</tr>
$highPriorityRows
</table>
</div>
</div>
"@
}
# Generate Top Opportunities section
$opportunitiesHtml = ""
if ($ReportInfo.ImprovementOpportunities.Count -gt 0) {
$opportunitiesRows = ($ReportInfo.ImprovementOpportunities | Select-Object -First 20 | ForEach-Object {
$safeTitle = ConvertTo-HtmlSafe $_.Title
$cleanRemediation = Strip-HtmlTags $_.Remediation
$safeRemediation = ConvertTo-HtmlSafe $cleanRemediation
$safeUserImpact = ConvertTo-HtmlSafe $_.UserImpact
$actionUrl = if ($_.ActionUrl) { ConvertTo-HtmlSafe $_.ActionUrl } else { "#" }
$costClass = switch ($_.ImplementationCost) {
"Low" { "success" }
"Moderate" { "warning" }
"High" { "failure" }
default { "" }
}
$statusClass = switch ($_.ImplementationStatus) {
"Implemented" { "success" }
"Not Implemented" { "failure" }
"Partial" { "warning" }
default { "" }
}
@"
<tr>
<td><strong>$safeTitle</strong><br><small style="color: #666;">$($safeRemediation.Substring(0, [Math]::Min(250, $safeRemediation.Length)))$(if ($safeRemediation.Length -gt 250) { "..." })</small></td>
<td style="text-align: center; font-weight: bold;">$($_.MaxScore)</td>
<td style="text-align: center;">$($_.CurrentScore)</td>
<td style="text-align: center;" class="$statusClass">$($_.PercentageComplete)%</td>
<td style="text-align: center;" class="$costClass">$($_.ImplementationCost)</td>
<td><small style="color: #666;">$safeUserImpact</small></td>
<td style="text-align: center;">$($_.ControlCategory)</td>
<td style="text-align: center;"><a href="$actionUrl" target="_blank" style="padding: 4px 12px; background: #0078D4; color: white; text-decoration: none; border-radius: 4px; font-size: 0.85em;">Configure</a></td>
</tr>
"@
}) -join ""
$opportunitiesHtml = @"
<div class="card">
<h2>📊 All Improvement Opportunities (Top 20 by Points)</h2>
<p style="color: #666;">Complete list of available security improvements sorted by potential score gain.</p>
<div class="table-container">
<table>
<tr>
<th>Control & Remediation Steps</th>
<th style="text-align: center; width: 70px;">Max<br>Points</th>
<th style="text-align: center; width: 70px;">Current</th>
<th style="text-align: center; width: 90px;">Progress</th>
<th style="text-align: center; width: 100px;">Cost</th>
<th style="width: 150px;">User Impact</th>
<th style="text-align: center; width: 90px;">Category</th>
<th style="text-align: center; width: 90px;">Action</th>
</tr>
$opportunitiesRows
</table>
</div>
<p style="margin-top: 10px; color: #666;"><em>Showing top 20 by maximum score. See CSV export for complete list.</em></p>
</div>
"@
}
# Generate Category Summary
$categorySummaryRows = ($ReportInfo.ControlsByCategory | ForEach-Object {
$categoryName = $_.Name
$totalControls = $_.Count
$implemented = ($_.Group | Where-Object { $_.ImplementationStatus -eq "Implemented" }).Count
$notImplemented = ($_.Group | Where-Object { $_.ImplementationStatus -eq "Not Implemented" }).Count
$partial = ($_.Group | Where-Object { $_.ImplementationStatus -eq "Partial" }).Count
$unknown = $totalControls - $implemented - $notImplemented - $partial
$avgCompletion = [math]::Round(($_.Group | Measure-Object -Property PercentageComplete -Average).Average, 1)
$totalPoints = [math]::Round(($_.Group | Measure-Object -Property CurrentScore -Sum).Sum, 0)
$maxPoints = [math]::Round(($_.Group | Measure-Object -Property MaxScore -Sum).Sum, 0)
@"
<tr>
<td>$categoryName</td>
<td style="text-align: center;">$totalControls</td>
<td style="text-align: center;" class="success">$implemented</td>
<td style="text-align: center;" class="failure">$notImplemented</td>
<td style="text-align: center;" class="warning">$partial</td>
<td style="text-align: center;">$unknown</td>
<td style="text-align: center; font-weight: bold;">$avgCompletion%</td>
<td style="text-align: center;">$totalPoints / $maxPoints</td>
</tr>
"@
}) -join ""
# HTML Template
$html = @"
<!DOCTYPE html>
<html>
<head>
<title>Microsoft 365 Secure Score Report - $TenantName</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
color: #333;
}
h1, h2, h3 {
color: #0078D4;
margin-top: 0;
}
.container {
max-width: 1600px;
margin: 0 auto;
padding: 20px;
}
.card {
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
padding: 20px;
margin-bottom: 20px;
overflow: hidden;
}
.header-card {
background: linear-gradient(135deg, #0078D4, #005a9e);
color: white;
}
.header-card h1 {
color: white;
}
.dashboard {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-bottom: 20px;
}
.dashboard-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 8px;
text-align: center;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.dashboard-number {
font-size: 2.2em;
font-weight: bold;
margin-bottom: 10px;
}
.dashboard-label {
font-size: 1em;
opacity: 0.9;
}
table {
border-collapse: collapse;
width: 100%;
margin-bottom: 10px;
font-size: 0.85rem;
}
th, td {
padding: 10px;
text-align: left;
border-bottom: 1px solid #ddd;
vertical-align: top;
}
th {
background-color: #0078D4;
color: white;
position: sticky;
top: 0;
font-weight: 600;
z-index: 10;
}
tr:hover {
background-color: #f5f5f5;
}
.success { color: #107C10; font-weight: bold; }
.warning { color: #FF8C00; font-weight: bold; }
.failure { color: #E81123; font-weight: bold; }
.table-container {
max-height: 700px;
overflow-y: auto;
margin-bottom: 10px;
}
.trend-up { color: #107C10; }
.trend-down { color: #E81123; }
.trend-neutral { color: #666; }
.info-section {
background-color: #E5F1FA;
padding: 15px;
border-radius: 5px;
margin: 15px 0;
}
a {
color: #0078D4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<div class="card header-card">
<h1>Microsoft 365 Secure Score Report</h1>
<div class="info-section" style="background-color: rgba(255,255,255,0.1);">
<strong>Tenant:</strong> $TenantName<br>
<strong>Domain:</strong> $TenantDomain<br>
<strong>Generated:</strong> $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")<br>
<strong>Last Score Update:</strong> $([DateTime]::Parse($ReportInfo.LastUpdated).ToString("yyyy-MM-dd HH:mm:ss"))
</div>
</div>
<div class="dashboard">
<div class="dashboard-card">
<div class="dashboard-number">$($ReportInfo.CurrentScore) / $($ReportInfo.MaxPossibleScore)</div>
<div class="dashboard-label">Current Score</div>
<div style="font-size: 1.3em; margin-top: 5px; font-weight: bold;">$($ReportInfo.PercentComplete)%</div>
</div>
<div class="dashboard-card">
<div class="dashboard-number $(if ($ReportInfo.ScoreTrend -gt 0) { "trend-up" } elseif ($ReportInfo.ScoreTrend -lt 0) { "trend-down" } else { "trend-neutral" })">
$(if ($ReportInfo.ScoreTrend -gt 0) { "+$($ReportInfo.ScoreTrend)" } else { "$($ReportInfo.ScoreTrend)" })
</div>
<div class="dashboard-label">Trend (90 days)</div>
<div style="font-size: 0.9em; margin-top: 5px;">Avg: $($ReportInfo.AverageScore)</div>
</div>
<div class="dashboard-card">
<div class="dashboard-number">$($ReportInfo.ImplementedControls)</div>
<div class="dashboard-label">Implemented</div>
<div style="font-size: 0.9em; margin-top: 5px;">of $($ReportInfo.AllControls.Count) total</div>
</div>
<div class="dashboard-card">
<div class="dashboard-number">$($ReportInfo.NotImplementedControls)</div>
<div class="dashboard-label">Not Implemented</div>
<div style="font-size: 0.9em; margin-top: 5px;">$($ReportInfo.PartialControls) partial</div>
</div>
<div class="dashboard-card">
<div class="dashboard-number">$($ReportInfo.QuickWins.Count)</div>
<div class="dashboard-label">Quick Wins</div>
<div style="font-size: 0.9em; margin-top: 5px;">Low effort, high value</div>
</div>
</div>
$quickWinsHtml
$highPriorityHtml
$opportunitiesHtml
<div class="card">
<h2>📈 Summary by Category</h2>
<div class="table-container">
<table>
<tr>
<th>Category</th>
<th style="text-align: center; width: 80px;">Total</th>
<th style="text-align: center; width: 100px;">Implemented</th>
<th style="text-align: center; width: 120px;">Not Implemented</th>
<th style="text-align: center; width: 80px;">Partial</th>
<th style="text-align: center; width: 90px;">Unknown</th>
<th style="text-align: center; width: 110px;">Avg Complete</th>
<th style="text-align: center; width: 120px;">Points</th>
</tr>
$categorySummaryRows
</table>
</div>
</div>
<div class="card">
<h3>📝 Understanding This Report</h3>
<ul>
<li><strong>Secure Score:</strong> Microsoft 365 Secure Score measures your organization's security posture. Higher scores indicate better security.</li>
<li><strong>Quick Wins:</strong> Low-to-moderate effort security improvements that provide significant value. These are your best ROI opportunities.</li>
<li><strong>High Priority:</strong> Microsoft's top-ranked recommendations based on current threat intelligence and industry best practices.</li>
<li><strong>Implementation Cost:</strong> Estimated effort - <span class="success">Low</span> (minimal), <span class="warning">Moderate</span> (some effort), <span class="failure">High</span> (significant resources).</li>
<li><strong>User Impact:</strong> How implementing each control affects end users.</li>
<li><strong>Action Links:</strong> Click "Configure" to go directly to the relevant admin console.</li>
<li><strong>Complete Details:</strong> See the exported CSV file for full control details including compliance mappings.</li>