Tracking deleted applications is crucial for maintaining the security and integrity of your Microsoft 365 environment. Using Microsoft Graph PowerShell, you can easily monitor "Delete application" events under the "ApplicationManagement" category. In this article, we'll provide a script to query these events, explain how the script works, suggest enhancements, cover use cases, highlight possible errors and their solutions, and wrap it all up with a conclusion.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes AuditLog.Read.All
# Define the filter
$filter = "activityDisplayName eq 'Delete application' and category eq 'ApplicationManagement'"
# Fetch the audit logs
$logs = Get-MgAuditLogDirectoryAudit -All `
-Filter $filter `
-Property activityDateTime, activityDisplayName, initiatedBy, result, targetResources
# Parse and output the results
$logs | ForEach-Object {
[PSCustomObject]@{
"Deleted Time" = $_.activityDateTime
"Deleted Application" = ($_.targetResources | Where-Object {$_.Type -eq 'Application'}).displayName
"Deleted By (Initiator UPN)" = $_.initiatedBy.user.userPrincipalName
"Result Status" = $_.result
}
} | Format-Table -AutoSize
You can enhance this script by:
Error | Cause | Solution |
Insufficient privileges to complete the operation. | The user account lacks the necessary permissions. | Ensure you connect with an account that has the AuditLog.Read.All permission. |
No audit records found. | No deletions match the filter or time period. | Verify that deletions occurred and adjust the timeframe if necessary. |
Connect-MgGraph : Access token validation failure. | Session expired or incorrect tenant selected. | Re-run Connect-MgGraph and ensure correct tenant context. |
Target resources array is empty. | No application resource linked to the event. | Add a fallback mechanism to handle missing target resources gracefully. |
Keeping track of deleted applications is vital for securing your Microsoft 365 environment and ensuring compliance. The provided Microsoft Graph PowerShell script offers a simple yet powerful way to monitor these critical events. With minor enhancements, such as automated reporting and alerting, you can build a comprehensive application governance and security framework. Regularly monitoring these activities helps prevent unauthorized actions and ensures that every change in your environment is tracked and accountable.
© m365corner.com. All Rights Reserved. Design by HTML Codex