-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMSGraphAuditsDownload.ps1
More file actions
187 lines (167 loc) · 6.71 KB
/
Copy pathMSGraphAuditsDownload.ps1
File metadata and controls
187 lines (167 loc) · 6.71 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
# ------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ------------------------------------------------------------
Import-Module Azure
# Replace with your tenantId or tenantDomain
$tenantId = ""
# Replace with your desired time ranges
$toDate = "{0:s}" -f (get-date).ToUniversalTime() + "Z"
$fromDate = "{0:s}" -f (get-date).AddDays(-7).ToUniversalTime() + "Z"
# You can add more filters here
$url = "https://graph.microsoft.com/beta/auditLogs/directoryAudits?`$filter=activityDateTime ge $fromDate and activityDateTime le $toDate"
# By default, it saves the result to DownloadedReport_currentTime.csv. Change it to different file name as needed.
$now = "{0:yyyyMMdd_hhmmss}" -f (get-date)
$outputFile = ".\AAD_AuditReport_$now.csv"
###################################
#### DO NOT MODIFY BELOW LINES ####
###################################
Function ConvertTo-FlatObject {
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeLine)][Object[]]$Objects,
[String]$Separator = ".",
[ValidateSet("", 0, 1)]$Base = 1,
[int]$Depth = 5,
[Parameter(DontShow)][String[]]$Path,
[Parameter(DontShow)][System.Collections.IDictionary] $OutputObject
)
Begin {
$InputObjects = [System.Collections.Generic.List[Object]]::new()
}
Process {
foreach ($O in $Objects) {
$InputObjects.Add($O)
}
}
End {
If ($PSBoundParameters.ContainsKey("OutputObject")) {
$Object = $InputObjects[0]
$Iterate = [ordered] @{}
if ($null -eq $Object) {
#Write-Verbose -Message "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()) {
$Iterate["$i"] = $Item
$i += 1
}
} else {
foreach ($Prop in $Object.PSObject.Properties) {
if ($Prop.IsGettable) {
$Iterate["$($Prop.Name)"] = $Object.$($Prop.Name)
}
}
}
}
If ($Iterate.Keys.Count) {
foreach ($Key in $Iterate.Keys) {
ConvertTo-FlatObject -Objects @(, $Iterate["$Key"]) -Separator $Separator -Base $Base -Depth $Depth -Path ($Path + $Key) -OutputObject $OutputObject
}
} else {
$Property = $Path -Join $Separator
$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
[PSCustomObject] $OutputObject
}
}
}
}
Function Get-Headers {
param( $token )
Return @{
"Authorization" = ("Bearer {0}" -f $token);
"Content-Type" = "application/json";
}
}
$clientId = "1b730954-1685-4b74-9bfd-dac224a7b894" # Azure Active Directory PowerShell clientId
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$MSGraphURI = "https://graph.microsoft.com"
$authority = "https://login.microsoftonline.com/$tenantId"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$authResult = $authContext.AcquireToken($MSGraphURI, $clientId, $redirectUri, "Always")
$token = $authResult.AccessToken
if ($token -eq $null) {
Write-Output "ERROR: Failed to get an Access Token"
exit
}
Write-Output "--------------------------------------------------------------"
Write-Output "Downloading report from $url"
Write-Output "Output file: $outputFile"
Write-Output "--------------------------------------------------------------"
# Call Microsoft Graph
$headers = Get-Headers($token)
$count=0
$retryCount = 0
$oneSuccessfulFetch = $False
Do {
Write-Output "Fetching data using Url: $url"
Try {
$myReport = (Invoke-WebRequest -UseBasicParsing -Headers $headers -Uri $url)
$convertedReport = ($myReport.Content | ConvertFrom-Json).value
$convertedReport | ConvertTo-FlatObject | ConvertTo-Csv -NoTypeInformation | Add-Content $outputFile
$url = ($myReport.Content | ConvertFrom-Json).'@odata.nextLink'
$count = $count+$convertedReport.Count
Write-Output "Total Fetched: $count"
$oneSuccessfulFetch = $True
$retryCount = 0
}
Catch [System.Net.WebException] {
$statusCode = [int]$_.Exception.Response.StatusCode
Write-Output $statusCode
Write-Output $_.Exception.Message
if($statusCode -eq 401 -and $oneSuccessfulFetch)
{
# Token might have expired! Renew token and try again
$authResult = $authContext.AcquireToken($MSGraphURI, $clientId, $redirectUri, "Auto")
$token = $authResult.AccessToken
$headers = Get-Headers($token)
$oneSuccessfulFetch = $False
}
elseif($statusCode -eq 429)
{
# throttled request, wait for a few seconds and retry
Start-Sleep -s 5
}
elseif($statusCode -eq 403 -or $statusCode -eq 400 -or $statusCode -eq 401)
{
Write-Output "Please check the permissions of the user"
break;
}
else {
if ($retryCount -lt 5) {
Write-Output "Retrying..."
$retryCount++
}
else {
Write-Output "Download request failed. Please try again in the future."
break
}
}
}
Catch {
$exType = $_.Exception.GetType().FullName
$exMsg = $_.Exception.Message
Write-Output "Exception: $_.Exception"
Write-Output "Error Message: $exType"
Write-Output "Error Message: $exMsg"
if ($retryCount -lt 5) {
Write-Output "Retrying..."
$retryCount++
}
else {
Write-Output "Download request failed. Please try again in the future."
break
}
}
Write-Output "--------------------------------------------------------------"
} while(-not[string]::IsNullOrEmpty($url))