@@ -10,78 +10,123 @@ function Invoke-ExecAuditLogSearch {
1010
1111 $APIName = $Request.Params.CIPPEndpoint
1212 $Headers = $Request.Headers
13+ $Action = $Request.Query.Action ?? $Request.Body.Action
14+
1315 Write-LogMessage - headers $Headers - API $APIName - message ' Accessed this API' - Sev ' Debug'
1416
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+ }
3028
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 )
3731
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
4344
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+ Write-LogMessage - headers $Headers - API $APIName - message " Queued search for processing: $ ( $Search.displayName ) " - Sev ' Info' - tenant $TenantFilter
46+
47+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
48+ StatusCode = [HttpStatusCode ]::OK
49+ Body = @ {
50+ resultText = " Search '$ ( $Search.displayName ) ' queued for processing."
51+ state = ' success'
52+ } | ConvertTo-Json - Depth 10 - Compress
53+ })
4954 }
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- }
55+ default {
56+ $Query = $Request.Body
57+ if (! $Query.TenantFilter ) {
58+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
59+ StatusCode = [HttpStatusCode ]::BadRequest
60+ Body = ' TenantFilter is required'
61+ })
62+ return
63+ }
64+ if (! $Query.StartTime -or ! $Query.EndTime ) {
65+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
66+ StatusCode = [HttpStatusCode ]::BadRequest
67+ Body = ' StartTime and EndTime are required'
68+ })
69+ return
70+ }
5871
59- try {
60- Write-Information " Executing audit log search with parameters: $ ( $Query | ConvertTo-Json - Depth 10 ) "
72+ # Convert StartTime and EndTime to DateTime from unixtime
73+ if ($Query.StartTime -match ' ^\d+$' ) {
74+ $Query.StartTime = [DateTime ]::UnixEpoch.AddSeconds([long ]$Query.StartTime )
75+ } else {
76+ $Query.StartTime = [DateTime ]$Query.StartTime
77+ }
6178
62- $Query = $Query | ConvertTo-Json - Depth 10 | ConvertFrom-Json - AsHashtable
63- $NewSearch = New-CippAuditLogSearch @Query
79+ if ($Query.EndTime -match ' ^\d+$' ) {
80+ $Query.EndTime = [DateTime ]::UnixEpoch.AddSeconds([long ]$Query.EndTime )
81+ } else {
82+ $Query.EndTime = [DateTime ]$Query.EndTime
83+ }
6484
65- if ($NewSearch ) {
66- $Results = @ {
67- resultText = " Created audit log search: $ ( $NewSearch.displayName ) "
68- state = ' success'
69- details = $NewSearch
85+ $Command = Get-Command New-CippAuditLogSearch
86+ $AvailableParameters = $Command.Parameters.Keys
87+ $BadProps = foreach ($Prop in $Query.PSObject.Properties.Name ) {
88+ if ($AvailableParameters -notcontains $Prop ) {
89+ $Prop
90+ }
7091 }
71- } else {
72- $Results = @ {
73- resultText = ' Failed to initiate search'
74- state = ' error'
92+ if ($BadProps ) {
93+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
94+ StatusCode = [HttpStatusCode ]::BadRequest
95+ Body = " Invalid parameters: $ ( $BadProps -join ' , ' ) "
96+ })
97+ return
98+ }
99+
100+ try {
101+ Write-Information " Executing audit log search with parameters: $ ( $Query | ConvertTo-Json - Depth 10 ) "
102+
103+ $Query = $Query | ConvertTo-Json - Depth 10 | ConvertFrom-Json - AsHashtable
104+ $NewSearch = New-CippAuditLogSearch @Query
105+
106+ if ($NewSearch ) {
107+ Write-LogMessage - headers $Headers - API $APIName - message " Created audit log search: $ ( $NewSearch.displayName ) " - Sev ' Info' - tenant $TenantFilter
108+ $Results = @ {
109+ resultText = " Created audit log search: $ ( $NewSearch.displayName ) "
110+ state = ' success'
111+ details = $NewSearch
112+ }
113+ } else {
114+ Write-LogMessage - headers $Headers - API $APIName - message ' Failed to create audit log search' - Sev ' Error' - tenant $TenantFilter
115+ $Results = @ {
116+ resultText = ' Failed to initiate search'
117+ state = ' error'
118+ }
119+ }
120+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
121+ StatusCode = [HttpStatusCode ]::OK
122+ Body = $Results
123+ })
124+ } catch {
125+ Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
126+ StatusCode = [HttpStatusCode ]::BadRequest
127+ Body = $_.Exception.Message
128+ })
75129 }
76130 }
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- })
86131 }
87132}
0 commit comments