-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path93-Get-SecureScoreInformation.ps1
More file actions
404 lines (361 loc) · 16.2 KB
/
93-Get-SecureScoreInformation.ps1
File metadata and controls
404 lines (361 loc) · 16.2 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
# Bitpusher
# \`._,'/
# (_- -_)
# \o/
# The Digital
# Fox
# https://theTechRelay.com
# https://github.com/bitpusher2k
#
# Get-SecureScoreInformation.ps1 - By Bitpusher/The Digital Fox
# v3.1 last updated 2025-07-26
# Script to retrieve and list M365 Secure Score information for a tenant using Graph API.
# Currently only exports JSON files.
#
# Usage:
# powershell -executionpolicy bypass -f .\Get-SecureScoreInformation.ps1 -OutputPath "Default" -UserIds "compromisedaccount@contoso.com" -DaysAgo "10"
#
# Run with already existing connection to M365 tenant through
# PowerShell modules.
#
# Uses Microsoft Graph commands.
#
#comp #m365 #security #bec #script #irscript #powershell
#Requires -Version 5.1
Param (
[string]$InputFile,
[string]$OutputPath = "Default",
[string]$UserIds,
[int]$DaysAgo,
[datetime]$StartDate,
[datetime]$EndDate,
[string]$scriptName = "Get-SecureScoreInformation",
[string]$Priority = "Normal",
[string]$DebugPreference = "SilentlyContinue",
[string]$VerbosePreference = "SilentlyContinue",
[string]$InformationPreference = "Continue",
[string]$logFileFolderPath = "C:\temp\log",
[string]$ComputerName = $env:computername,
[string]$ScriptUserName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name,
[string]$logFilePrefix = "$scriptName" + "_" + "$ComputerName" + "_",
[string]$logFileDateFormat = "yyyyMMdd_HHmmss",
[int]$logFileRetentionDays = 30,
[string]$Encoding = "utf8NoBOM" # PS 5 & 7: "Ascii" (7-bit), "BigEndianUnicode" (UTF-16 big-endian), "BigEndianUTF32", "Oem", "Unicode" (UTF-16 little-endian), "UTF32" (little-endian), "UTF7", "UTF8" (PS 5: BOM, PS 7: NO BOM). PS 7: "ansi", "utf8BOM", "utf8NoBOM"
)
#region initialization
if ($PSVersionTable.PSVersion.Major -eq 5 -and ($Encoding -eq "utf8bom" -or $Encoding -eq "utf8nobom")) { $Encoding = "utf8" }
function Get-TimeStamp {
param(
[switch]$NoWrap,
[switch]$Utc
)
$dt = Get-Date
if ($Utc -eq $true) {
$dt = $dt.ToUniversalTime()
}
$str = "{0:yyyy-MM-dd} {0:HH:mm:ss}" -f $dt
if ($NoWrap -ne $true) {
$str = "[$str]"
}
return $str
}
if ($logFileFolderPath -ne "") {
if (!(Test-Path -PathType Container -Path $logFileFolderPath)) {
Write-Output "$(Get-TimeStamp) Creating directory $logFileFolderPath" | Out-Null
New-Item -ItemType Directory -Force -Path $logFileFolderPath | Out-Null
} else {
$DatetoDelete = $(Get-Date).AddDays(- $logFileRetentionDays)
Get-ChildItem $logFileFolderPath | Where-Object { $_.Name -like "*$logFilePrefix*" -and $_.LastWriteTime -lt $DatetoDelete } | Remove-Item | Out-Null
}
$logFilePath = $logFileFolderPath + "\$logFilePrefix" + (Get-Date -Format $logFileDateFormat) + ".LOG"
}
$sw = [Diagnostics.StopWatch]::StartNew()
Write-Output "$scriptName started on $ComputerName by $ScriptUserName at $(Get-TimeStamp)" | Tee-Object -FilePath $logFilePath -Append
$process = Get-Process -Id $pid
Write-Output "Setting process priority to `"$Priority`"" | Tee-Object -FilePath $logFilePath -Append
$process.PriorityClass = $Priority
#endregion initialization
function ConvertTo-FlatObject {
<# https://evotec.xyz/powershell-converting-advanced-object-to-flat-object/
.SYNOPSIS
Flattens a nested object into a single level object.
.DESCRIPTION
Flattens a nested object into a single level object.
.PARAMETER Objects
The object (or objects) to be flatten.
.PARAMETER Separator
The separator used between the recursive property names
.PARAMETER Base
The first index name of an embedded array:
- 1, arrays will be 1 based: <Parent>.1, <Parent>.2, <Parent>.3, ...
- 0, arrays will be 0 based: <Parent>.0, <Parent>.1, <Parent>.2, ...
- "", the first item in an array will be unnamed and than followed with 1: <Parent>, <Parent>.1, <Parent>.2, ...
.PARAMETER Depth
The maximal depth of flattening a recursive property. Any negative value will result in an unlimited depth and could cause a infinitive loop.
.PARAMETER Uncut
The maximal depth of flattening a recursive property. Any negative value will result in an unlimited depth and could cause a infinitive loop.
.PARAMETER ExcludeProperty
The properties to be excluded from the output.
.EXAMPLE
$Object3 = [PSCustomObject] @{
"Name" = "Przemyslaw Klys"
"Age" = "30"
"Address" = @{
"Street" = "Kwiatowa"
"City" = "Warszawa"
"Country" = [ordered] @{
"Name" = "Poland"
}
List = @(
[PSCustomObject] @{
"Name" = "Adam Klys"
"Age" = "32"
}
[PSCustomObject] @{
"Name" = "Justyna Klys"
"Age" = "33"
}
[PSCustomObject] @{
"Name" = "Justyna Klys"
"Age" = 30
}
[PSCustomObject] @{
"Name" = "Justyna Klys"
"Age" = $null
}
)
}
ListTest = @(
[PSCustomObject] @{
"Name" = "Slawa Klys"
"Age" = "33"
}
)
}
$Object3 | ConvertTo-FlatObject
.NOTES
Based on https://powersnippets.com/convertto-flatobject/
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeLine)] [Object[]]$Objects,
[string]$Separator = ".",
[ValidateSet("", 0, 1)] $Base = 1,
[int]$Depth = 10,
[string[]]$ExcludeProperty,
[Parameter(DontShow)] [String[]]$Path,
[Parameter(DontShow)] [System.Collections.IDictionary]$OutputObject
)
begin {
$InputObjects = [System.Collections.Generic.List[Object]]::new()
}
process {
foreach ($O in $Objects) {
if ($null -ne $O) {
$InputObjects.Add($O)
}
}
}
end {
if ($PSBoundParameters.ContainsKey("OutputObject")) {
$Object = $InputObjects[0]
$Iterate = [ordered]@{}
if ($null -eq $Object) {
#Write-Output "ConvertTo-FlatObject - Object is null"
} elseif ($Object.GetType().Name -in 'String', 'DateTime', 'TimeSpan', 'Version', 'Enum') {
$Object = $Object.ToString()
} elseif ($Depth) {
$Depth --
if ($Object -is [System.Collections.IDictionary]) {
$Iterate = $Object
} elseif ($Object -is [array] -or $Object -is [System.Collections.IEnumerable]) {
$i = $Base
foreach ($Item in $Object.GetEnumerator()) {
$NewObject = [ordered]@{}
if ($Item -is [System.Collections.IDictionary]) {
foreach ($Key in $Item.Keys) {
if ($Key -notin $ExcludeProperty) {
$NewObject[$Key] = $Item[$Key]
}
}
} elseif ($Item -isnot [array] -and $Item -isnot [System.Collections.IEnumerable]) {
foreach ($Prop in $Item.PSObject.Properties) {
if ($Prop.IsGettable -and $Prop.Name -notin $ExcludeProperty) {
$NewObject["$($Prop.Name)"] = $Item.$($Prop.Name)
}
}
} else {
$NewObject = $Item
}
$Iterate["$i"] = $NewObject
$i += 1
}
} else {
foreach ($Prop in $Object.PSObject.Properties) {
if ($Prop.IsGettable -and $Prop.Name -notin $ExcludeProperty) {
$Iterate["$($Prop.Name)"] = $Object.$($Prop.Name)
}
}
}
}
if ($Iterate.Keys.Count) {
foreach ($Key in $Iterate.Keys) {
if ($Key -notin $ExcludeProperty) {
ConvertTo-FlatObject -Objects @(, $Iterate["$Key"]) -Separator $Separator -Base $Base -Depth $Depth -Path ($Path + $Key) -OutputObject $OutputObject -ExcludeProperty $ExcludeProperty
}
}
} else {
$Property = $Path -join $Separator
if ($Property) {
# We only care if property is not empty
if ($Object -is [System.Collections.IDictionary] -and $Object.Keys.Count -eq 0) {
$OutputObject[$Property] = $null
} else {
$OutputObject[$Property] = $Object
}
}
}
} elseif ($InputObjects.Count -gt 0) {
foreach ($ItemObject in $InputObjects) {
$OutputObject = [ordered]@{}
ConvertTo-FlatObject -Objects @(, $ItemObject) -Separator $Separator -Base $Base -Depth $Depth -Path $Path -OutputObject $OutputObject -ExcludeProperty $ExcludeProperty
[pscustomobject]$OutputObject
}
}
}
}
$date = Get-Date -Format "yyyyMMddHHmmss"
$ScopeCheck = (Get-MgContext).Scopes -contains "Policy.Read.All"
if (!$ScopeCheck) {
Write-Output "User.Read.All scope not found in current context. Press enter to connect with broader scopes, or press Ctrl+c to exit." | Tee-Object -FilePath $logFilePath -Append
Pause
Connect-MgGraph -Scopes "UserAuthenticationMethod.ReadWrite.All", "Directory.ReadWrite.All", "User.ReadWrite.All", "Group.ReadWrite.All", "GroupMember.Read.All", "Policy.Read.All", "Policy.ReadWrite.ConditionalAccess", "Application.ReadWrite.All", "Files.ReadWrite.All", "Sites.ReadWrite.All", "AuditLog.Read.All", "Agreement.Read.All", "IdentityRiskEvent.Read.All", "IdentityRiskyUser.ReadWrite.All", "Mail.Send", "Mail.Read", "SecurityEvents.ReadWrite.All", "Directory.AccessAsUser.All", "AppRoleAssignment.ReadWrite.All", "AuditLogsQuery.Read.All"
}
## If OutputPath variable is not defined, prompt for it
if (!$OutputPath) {
Write-Output ""
$OutputPath = Read-Host "Enter the output base path, e.g. $($env:userprofile)\Desktop\Investigation (default)" | Tee-Object -FilePath $logFilePath -Append
If ($OutputPath -eq '') { $OutputPath = "$($env:userprofile)\Desktop\Investigation" }
Write-Output "Output base path will be in $OutputPath" | Tee-Object -FilePath $logFilePath -Append
} elseif ($OutputPath -eq 'Default') {
Write-Output ""
$OutputPath = "$($env:userprofile)\Desktop\Investigation"
Write-Output "Output base path will be in $OutputPath" | Tee-Object -FilePath $logFilePath -Append
}
## If OutputPath does not exist, create it
$CheckOutputPath = Get-Item $OutputPath -ErrorAction SilentlyContinue
if (!$CheckOutputPath) {
Write-Output ""
Write-Output "Output path does not exist. Directory will be created." | Tee-Object -FilePath $logFilePath -Append
mkdir $OutputPath
}
## Get Primary Domain Name for output subfolder
# $PrimaryDomain = Get-AcceptedDomain | Where-Object Default -eq $true
# $DomainName = $PrimaryDomain.DomainName
$PrimaryDomain = Get-MgDomain | Where-Object { $_.isdefault -eq $True } | Select-Object -Property ID
if ($PrimaryDomain) {
$DomainName = $PrimaryDomain.ID
} else {
$DomainName = "DefaultOutput"
}
$CheckSubDir = Get-Item $OutputPath\$DomainName -ErrorAction SilentlyContinue
if (!$CheckSubDir) {
Write-Output ""
Write-Output "Domain sub-directory does not exist. Sub-directory `"$DomainName`" will be created." | Tee-Object -FilePath $logFilePath -Append
mkdir $OutputPath\$DomainName
}
Write-Output "Domain sub-directory will be `"$DomainName`"" | Tee-Object -FilePath $logFilePath -Append
try {
$context = get-mgcontext -ErrorAction Stop
} catch {
write-output "Not connected to MgGraph. Ending."
exit
}
Write-Output "Connected to Microsoft Graph"
Write-Output "Connected account: $($context.Account) `n"
Get-MgBetaSecuritySecureScoreCount
$Scores = Get-MgBetaSecuritySecureScore -All
$Profiles = Get-MgBetaSecuritySecureScoreControlProfile -All
# Roughed out code to make Graph API queries directly:
#
# # Specify the URI to call and method
# $uri = "https://graph.microsoft.com/beta/security/securescores"
# $method = "GET"
#
# Write-Output "Run Graph API Query"
# # Run Graph API query
# $query = Invoke-MgGraphRequest -Uri $URI -method $method -ErrorAction Stop
#
# $names = $query.value[0].controlscores # get the most current secure score results
#
# $item = 0
# Write-Output "Display results`n"
# foreach ($control in $names) {
# $item++
# Write-Output -foregroundcolor green -BackgroundColor Black "`n*** Item", $item, "***"
# Write-Output "Control Category : ", $control.controlCategory
# Write-Output "Control Name : ", $control.controlName
# Write-Output "Control Score : ", $control.Score
# Write-Output "Control Description : ", $control.Description
# Write-Output "Control On : ", $control.on
# Write-Output "Implementation status: ", $control.implementationstatus
# Write-Output "Score in percentage : ", $control.scoreinpercentage
# Write-Output "Last synced : ", $control.lastsynced
# Write-Output "`n"
# }
# Write-Output "`nScript Completed`n"
#
# $item = 0
# $report = foreach ($control in $names) {
# $control.controlCategory
# $control.controlName
# $control.Score
# $control.Description
# $control.on
# $control.implementationstatus
# $control.scoreinpercentage
# $control.lastsynced
# }
#
# Write-Output "Making Graph Request..."
# $uri = "https://graph.microsoft.com/beta/security/securescores"
# $request = Invoke-MgGraphRequest -Uri $URI -method GET -ErrorAction Stop
#
# Write-Output "`nSecure score for last 90 days"
# Write-Output "-----------------------------`n"
# foreach ($item in $request.value) {
# $sspercent=($item.currentscore/$item.maxscore)
# $formattedDate = $item.createdDateTime.ToString("dd-MM-yyyy")
# Write-Output "$formattedDate Score =",$item.currentscore, "of",$item.maxscore,"["$sspercent.tostring("P")"]`n"
# }
# Write-Output "Script Finished`n"
# if ($debug) {
# Stop-Transcript | Out-Null
# }
Write-Output "Saving Secure Score information to files..." | Tee-Object -FilePath $logFilePath -Append
$OutputJSON = "$OutputPath\$DomainName\SecureScore_$($date).json"
# $OutputCSV = "$OutputPath\$DomainName\SecureScore_$($date).csv"
$ScoresJSON = $Scores | ConvertTo-Json -Depth 100
$ScoresJSON | Out-File -FilePath $OutputJSON -Encoding $Encoding
# $Scores | ConvertTo-FlatObject -Base 1 -Depth 20 | Export-Csv -Path $OutputCSV -NoTypeInformation -Encoding $Encoding
# [io.file]::readalltext("$OutputCSV").replace("System.Object[]","") | Out-File $OutputCSV -Encoding $Encoding –Force
$OutputJSON = "$OutputPath\$DomainName\SecureScoreProfile_$($date).json"
# $OutputCSV = "$OutputPath\$DomainName\SecureScoreProfile_$($date).csv"
$ProfilesJSON = $Profiles | ConvertTo-Json -Depth 100
$ProfilesJSON | Out-File -FilePath $OutputJSON -Encoding $Encoding
# $Profiles | ConvertTo-FlatObject -Base 1 -Depth 20 | Export-Csv -Path $OutputCSV -NoTypeInformation -Encoding $Encoding
# [io.file]::readalltext("$OutputCSV").replace("System.Object[]","") | Out-File $OutputCSV -Encoding $Encoding –Force
if ((Test-Path -Path $OutputJSON) -eq "True") {
Write-Output `n" The Output file is available at:" | Tee-Object -FilePath $logFilePath -Append
Write-Output $OutputJSON | Tee-Object -FilePath $logFilePath -Append
# $Prompt = New-Object -ComObject wscript.shell
# $UserInput = $Prompt.popup("Do you want to open output file?", 0, "Open Output File", 4)
# if ($UserInput -eq 6) {
# Invoke-Item "$OutputCSV"
# }
}
Write-Output "Script complete." | Tee-Object -FilePath $logFilePath -Append
Write-Output "Seconds elapsed for script execution: $($sw.elapsed.totalseconds)" | Tee-Object -FilePath $logFilePath -Append
Write-Output "`nDone! Check output path for results." | Tee-Object -FilePath $logFilePath -Append
Invoke-Item "$OutputPath\$DomainName"
Exit