Comprehensive Group and Mailbox Analysis


# Install necessary modules if not already installed
Install-Module -Name ExchangeOnlineManagement -Force -AllowClobber
Install-Module -Name Microsoft.Graph -Force -AllowClobber

# Import the modules
Import-Module ExchangeOnlineManagement
Import-Module Microsoft.Graph

# Certificate-Based Authentication Placeholder
# Replace with your authentication details
# Connect-ExchangeOnline -CertificateThumbprint "YOUR_CERTIFICATE_THUMBPRINT" -AppId "YOUR_APP_ID" -Organization "YOUR_TENANT_NAME"
# Connect-MgGraph -CertificateThumbprint "YOUR_CERTIFICATE_THUMBPRINT" -ClientId "YOUR_APP_ID" -TenantId "YOUR_TENANT_ID"

# Set the path and report name with date and time
$reportPath = "C:\Temp\"
$archivePath = Join-Path -Path $reportPath -ChildPath "archive"
if (!(Test-Path -Path $archivePath)) {
    New-Item -Path $archivePath -ItemType Directory
}
$reportFileName = "unified_report_$((Get-Date).ToString('yyyyMMdd_HHmmss')).csv"
$reportFullPath = Join-Path -Path $reportPath -ChildPath $reportFileName

# Check for previous report and move it to archive
$previousReport = Get-ChildItem -Path $reportPath -Filter "unified_report_*.csv" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($previousReport) {
    Move-Item -Path $previousReport.FullName -Destination $archivePath
}

# Retrieve data for the report
$distributionGroups = Get-EXORecipient -RecipientTypeDetails MailUniversalDistributionGroup,MailUniversalSecurityGroup,DynamicDistributionGroup -ResultSize Unlimited
$m365Groups = Get-EXOMailbox -RecipientTypeDetails GroupMailbox -ResultSize Unlimited
$sharedMailboxes = Get-EXOMailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited
$teamsGroups = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')" -ConsistencyLevel eventual -CountVariable teamsCount -All
$sharePointSites = Get-MgSite -ConsistencyLevel eventual -CountVariable siteCount -All

# Create a summary report
$reportSummary = @()
$reportSummary += [PSCustomObject]@{ Category = "Distribution Groups"; Count = $distributionGroups.Count }
$reportSummary += [PSCustomObject]@{ Category = "Microsoft 365 Groups"; Count = $m365Groups.Count }
$reportSummary += [PSCustomObject]@{ Category = "Shared Mailboxes"; Count = $sharedMailboxes.Count }
$reportSummary += [PSCustomObject]@{ Category = "Teams Groups"; Count = $teamsGroups.Count }
$reportSummary += [PSCustomObject]@{ Category = "SharePoint Sites"; Count = $sharePointSites.Count }

# Export the summary report to CSV
$reportSummary | Export-Csv -Path $reportFullPath -NoTypeInformation

# Compare with the previous report if it exists
$changeDetected = $false
$previousReportPath = Join-Path -Path $archivePath -ChildPath $previousReport.Name
$comparisonResults = @()

if (Test-Path -Path $previousReportPath) {
    $previousData = Import-Csv -Path $previousReportPath
    $currentData = Import-Csv -Path $reportFullPath

    $comparisonResults = Compare-Object -ReferenceObject $previousData -DifferenceObject $currentData -Property Category, Count
    if ($comparisonResults) {
        $changeDetected = $true
    }
}

# Create HTML report with change highlights
$htmlReport = @"
<html>
<head>
<style>
    table {width: 100%; border-collapse: collapse;}
    th, td {border: 1px solid black; padding: 8px; text-align: left;}
    .changed-yes {background-color: green; color: white;}
    .changed-no {background-color: red; color: white;}
</style>
</head>
<body>
<h2>Unified Report Summary</h2>
<table>
<tr>
    <th>Category</th>
    <th>Count</th>
    <th>Changed</th>
</tr>
"@

foreach ($item in $reportSummary) {
    $change = "No"
    $cssClass = "changed-no"

    if ($comparisonResults | Where-Object { $_.Category -eq $item.Category }) {
        $change = "Yes"
        $cssClass = "changed-yes"
    }

    $htmlReport += "<tr>"
    $htmlReport += "<td>$($item.Category)</td>"
    $htmlReport += "<td>$($item.Count)</td>"
    $htmlReport += "<td class='$cssClass'>$change</td>"
    $htmlReport += "</tr>"
}

$htmlReport += "</table></body></html>"

# Save the HTML report
$htmlReportPath = Join-Path -Path $reportPath -ChildPath "unified_report.html"
$htmlReport | Out-File -FilePath $htmlReportPath

# Send the email with the HTML report in the body and the CSV as an attachment
$recipient = "team@firstrepublic.com"
$subject = "Unified Group and Mailbox Report"
$body = Get-Content -Path $htmlReportPath -Raw
$attachment = $reportFullPath

Send-MailMessage -To $recipient -From "noreply@yourdomain.com" -Subject $subject -Body $body -BodyAsHtml -Attachments $attachment -SmtpServer "smtp.yourdomain.com"

# Disconnect from Exchange Online and Microsoft Graph
Disconnect-ExchangeOnline -Confirm:$false
Disconnect-MgGraph

test Exchange Administrator Tecism
Sorry! The Author has not filled his profile.
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments