Skip to content

Commit 2852c0a

Browse files
Merge pull request #636 from rvdwegen/dev
Add function to retrieve audit logs for a CA policy
2 parents acb6bb0 + d1559c5 commit 2852c0a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using namespace System.Net
2+
3+
Function Invoke-ListConditionalAccessPolicyChanges {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$APIName = $TriggerMetadata.FunctionName
12+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
13+
14+
# Write to the Azure Functions log stream.
15+
Write-Host 'PowerShell HTTP trigger function processed a request.'
16+
17+
# Interact with query parameters or the body of the request.
18+
$TenantFilter = $Request.Query.TenantFilter
19+
$policyId = $Request.body.id
20+
$policyDisplayName = $Request.body.displayName
21+
22+
try {
23+
[array]$changes = New-GraphGetRequest -uri "https://graph.microsoft.com/v1.0/auditLogs/directoryAudits?`$filter=targetResources/any(s:s/id eq '$($policyId)')" -tenantid $TenantFilter | ForEach-Object {
24+
[pscustomobject]@{
25+
policy = $policyDisplayName
26+
policyId = $policyId
27+
typeFriendlyName = $_.activityDisplayName
28+
type = $_.operationType
29+
initiatedBy = if ($_.initiatedBy.user.userPrincipalName) { $_.initiatedBy.user.userPrincipalName } else { $_.initiatedBy.app.displayName }
30+
date = $_.activityDateTime
31+
oldValue = ($_.targetResources[0].modifiedProperties.oldValue | ConvertFrom-Json) # targetResources is an array, can we ever get more than 1 object in it?
32+
newValue = ($_.targetResources[0].modifiedProperties.newValue | ConvertFrom-Json)
33+
}
34+
}
35+
$StatusCode = [HttpStatusCode]::OK
36+
} catch {
37+
$StatusCode = [HttpStatusCode]::BadRequest
38+
Write-Host $($_.Exception.message)
39+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APIName -message "Failed to request audit logs for policy $($policyDisplayName): $($_.Exception.message)" -Sev "Error" -tenant $TenantFilter
40+
}
41+
42+
# Associate values to output bindings by calling 'Push-OutputBinding'.
43+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
44+
StatusCode = $StatusCode
45+
Body = $changes
46+
})
47+
}

0 commit comments

Comments
 (0)