1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
[crayon–67904196b8c10842920817 inline=“true” ]# 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 |
[/crayon]
Sorry! The Author has not filled his profile.