Enhancing Security Compliance with PowerShell in M365

In the modern digital workplace, maintaining security compliance is paramount. This PowerShell script automates the retrieval of security compliance information from Microsoft 365, helping administrators to assess the state of their organizational compliance configurations quickly and efficiently.
This script will:
1. Connect to Microsoft 365 services.
2. Retrieve and display compliance details such as audit logs, data loss prevention (DLP) rules, and user permissions.
3. Generate a report outlining compliance settings.
By automating these compliance checks, organizations can ensure they are meeting necessary security requirements without the hassle of manual audits.

# Install the required module if not already installed
if (-Not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
    Install-Module Microsoft.Graph -Scope CurrentUser -AllowClobber
}
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "AuditLog.Read.All","Compliance"
# Get audit logs
$auditLogs = Get-MgAuditLogSignIn -Top 10
Write-Host "=== Recent Audit Logs ==="
foreach ($log in $auditLogs) {
    Write-Host "User: $($log.UserPrincipalName), Status: $($log.Status), Time: $($log.CreatedDateTime)"
}
# Get Data Loss Prevention (DLP) rules
$dlpRules = Get-MgDataLossPreventionPolicy
Write-Host "=== Data Loss Prevention Rules ==="
foreach ($rule in $dlpRules) {
    Write-Host "DLP Rule: $($rule.DisplayName), Status: $($rule.Status)"
}
# Get user permissions
$users = Get-MgUser
Write-Host "=== User Permissions ==="
foreach ($user in $users) {
    Write-Host "User: $($user.DisplayName), Roles: $($user.AssignedLicenses | Measure-Object).Count"
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Compliance report generation completed."