@@ -10,78 +10,119 @@ function Invoke-ExecAuditLogSearch {
10
10
11
11
$APIName = $Request.Params.CIPPEndpoint
12
12
$Headers = $Request.Headers
13
+ $Action = $Request.Query.Action ?? $Request.Body.Action
14
+
13
15
Write-LogMessage - headers $Headers - API $APIName - message ' Accessed this API' - Sev ' Debug'
14
16
15
- $Query = $Request.Body
16
- if (! $Query.TenantFilter ) {
17
- Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
18
- StatusCode = [HttpStatusCode ]::BadRequest
19
- Body = ' TenantFilter is required'
20
- })
21
- return
22
- }
23
- if (! $Query.StartTime -or ! $Query.EndTime ) {
24
- Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
25
- StatusCode = [HttpStatusCode ]::BadRequest
26
- Body = ' StartTime and EndTime are required'
27
- })
28
- return
29
- }
17
+ switch ($Action ) {
18
+ ' ProcessLogs' {
19
+ $SearchId = $Request.Query.SearchId ?? $Request.Body.SearchId
20
+ $TenantFilter = $Request.Query.tenantFilter ?? $Request.Body.tenantFilter
21
+ if (! $SearchId ) {
22
+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
23
+ StatusCode = [HttpStatusCode ]::BadRequest
24
+ Body = ' SearchId is required'
25
+ })
26
+ return
27
+ }
30
28
31
- # Convert StartTime and EndTime to DateTime from unixtime
32
- if ($Query.StartTime -match ' ^\d+$' ) {
33
- $Query.StartTime = [DateTime ]::UnixEpoch.AddSeconds([long ]$Query.StartTime )
34
- } else {
35
- $Query.StartTime = [DateTime ]$Query.StartTime
36
- }
29
+ $Search = New-GraphGetRequest - Uri " https://graph.microsoft.com/beta/security/auditLog/queries/$SearchId " - AsApp $true - TenantId $TenantFilter
30
+ Write-Information ($Search | ConvertTo-Json - Depth 10 )
37
31
38
- if ($Query.EndTime -match ' ^\d+$' ) {
39
- $Query.EndTime = [DateTime ]::UnixEpoch.AddSeconds([long ]$Query.EndTime )
40
- } else {
41
- $Query.EndTime = [DateTime ]$Query.EndTime
42
- }
32
+ $Entity = [PSCustomObject ]@ {
33
+ PartitionKey = [string ]' Search'
34
+ RowKey = [string ]$SearchId
35
+ Tenant = [string ]$TenantFilter
36
+ DisplayName = [string ]$Search.displayName
37
+ StartTime = [datetime ]$Search.filterStartDateTime
38
+ EndTime = [datetime ]$Search.filterEndDateTime
39
+ Query = [string ]($Search | ConvertTo-Json - Compress)
40
+ CippStatus = [string ]' Pending'
41
+ }
42
+ $Table = Get-CIPPTable - TableName ' AuditLogSearches'
43
+ Add-CIPPAzDataTableEntity @Table - Entity $Entity - Force | Out-Null
43
44
44
- $Command = Get-Command New-CippAuditLogSearch
45
- $AvailableParameters = $Command.Parameters.Keys
46
- $BadProps = foreach ($Prop in $Query.PSObject.Properties.Name ) {
47
- if ($AvailableParameters -notcontains $Prop ) {
48
- $Prop
45
+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
46
+ StatusCode = [HttpStatusCode ]::OK
47
+ Body = @ {
48
+ resultText = " Search '$ ( $Search.displayName ) ' queued for processing."
49
+ state = ' success'
50
+ } | ConvertTo-Json - Depth 10 - Compress
51
+ })
49
52
}
50
- }
51
- if ($BadProps ) {
52
- Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
53
- StatusCode = [HttpStatusCode ]::BadRequest
54
- Body = " Invalid parameters: $ ( $BadProps -join ' , ' ) "
55
- })
56
- return
57
- }
53
+ default {
54
+ $Query = $Request.Body
55
+ if (! $Query.TenantFilter ) {
56
+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
57
+ StatusCode = [HttpStatusCode ]::BadRequest
58
+ Body = ' TenantFilter is required'
59
+ })
60
+ return
61
+ }
62
+ if (! $Query.StartTime -or ! $Query.EndTime ) {
63
+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
64
+ StatusCode = [HttpStatusCode ]::BadRequest
65
+ Body = ' StartTime and EndTime are required'
66
+ })
67
+ return
68
+ }
58
69
59
- try {
60
- Write-Information " Executing audit log search with parameters: $ ( $Query | ConvertTo-Json - Depth 10 ) "
70
+ # Convert StartTime and EndTime to DateTime from unixtime
71
+ if ($Query.StartTime -match ' ^\d+$' ) {
72
+ $Query.StartTime = [DateTime ]::UnixEpoch.AddSeconds([long ]$Query.StartTime )
73
+ } else {
74
+ $Query.StartTime = [DateTime ]$Query.StartTime
75
+ }
61
76
62
- $Query = $Query | ConvertTo-Json - Depth 10 | ConvertFrom-Json - AsHashtable
63
- $NewSearch = New-CippAuditLogSearch @Query
77
+ if ($Query.EndTime -match ' ^\d+$' ) {
78
+ $Query.EndTime = [DateTime ]::UnixEpoch.AddSeconds([long ]$Query.EndTime )
79
+ } else {
80
+ $Query.EndTime = [DateTime ]$Query.EndTime
81
+ }
64
82
65
- if ($NewSearch ) {
66
- $Results = @ {
67
- resultText = " Created audit log search: $ ( $NewSearch.displayName ) "
68
- state = ' success'
69
- details = $NewSearch
83
+ $Command = Get-Command New-CippAuditLogSearch
84
+ $AvailableParameters = $Command.Parameters.Keys
85
+ $BadProps = foreach ($Prop in $Query.PSObject.Properties.Name ) {
86
+ if ($AvailableParameters -notcontains $Prop ) {
87
+ $Prop
88
+ }
70
89
}
71
- } else {
72
- $Results = @ {
73
- resultText = ' Failed to initiate search'
74
- state = ' error'
90
+ if ($BadProps ) {
91
+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
92
+ StatusCode = [HttpStatusCode ]::BadRequest
93
+ Body = " Invalid parameters: $ ( $BadProps -join ' , ' ) "
94
+ })
95
+ return
96
+ }
97
+
98
+ try {
99
+ Write-Information " Executing audit log search with parameters: $ ( $Query | ConvertTo-Json - Depth 10 ) "
100
+
101
+ $Query = $Query | ConvertTo-Json - Depth 10 | ConvertFrom-Json - AsHashtable
102
+ $NewSearch = New-CippAuditLogSearch @Query
103
+
104
+ if ($NewSearch ) {
105
+ $Results = @ {
106
+ resultText = " Created audit log search: $ ( $NewSearch.displayName ) "
107
+ state = ' success'
108
+ details = $NewSearch
109
+ }
110
+ } else {
111
+ $Results = @ {
112
+ resultText = ' Failed to initiate search'
113
+ state = ' error'
114
+ }
115
+ }
116
+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
117
+ StatusCode = [HttpStatusCode ]::OK
118
+ Body = $Results
119
+ })
120
+ } catch {
121
+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
122
+ StatusCode = [HttpStatusCode ]::BadRequest
123
+ Body = $_.Exception.Message
124
+ })
75
125
}
76
126
}
77
- Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
78
- StatusCode = [HttpStatusCode ]::OK
79
- Body = $Results
80
- })
81
- } catch {
82
- Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
83
- StatusCode = [HttpStatusCode ]::BadRequest
84
- Body = $_.Exception.Message
85
- })
86
127
}
87
128
}
0 commit comments