Skip to content

Test-MtLimitOnMicrosoftDomainUsage.ps1. Fix-licensing-and-mailbox-filter#1973

Merged
merill merged 5 commits into
maester365:mainfrom
JeanPhilippeGeorge:Test-MtLimitOnMicrosoftDomainUsage-Fix-licensing-and-mailbox-filter
Jul 22, 2026
Merged

Test-MtLimitOnMicrosoftDomainUsage.ps1. Fix-licensing-and-mailbox-filter#1973
merill merged 5 commits into
maester365:mainfrom
JeanPhilippeGeorge:Test-MtLimitOnMicrosoftDomainUsage-Fix-licensing-and-mailbox-filter

Conversation

@JeanPhilippeGeorge

@JeanPhilippeGeorge JeanPhilippeGeorge commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

📑 Test-MtLimitOnMicrosoftDomainUsage.ps1. Fix licensing and mailbox filter

Closes #1840

✅ Checks

  • My pull request adheres to the code style of this project.
  • The build and unit tests pass after running /powershell/tests/pester.ps1 locally.

Summary by CodeRabbit

  • Bug Fixes
    • Improved tenant capability detection for domain usage checks by using Microsoft 365 licensing details first, and only proceeding with Exchange Online checks after confirming connectivity.
    • Refined mailbox discovery to exclude discovery mailboxes and ensure primary addresses match the expected *@*.onmicrosoft.com pattern.
    • Strengthened Microsoft Graph user validation by only attempting lookups when a directory object ID is available, and reporting which matched mailboxes couldn’t be resolved in Graph.

@JeanPhilippeGeorge
JeanPhilippeGeorge requested a review from a team as a code owner July 21, 2026 08:09
@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c5128cf3-5ef4-4bc4-ab54-7e6ff955b496

📥 Commits

Reviewing files that changed from the base of the PR and between ea4dfaa and 830a364.

📒 Files selected for processing (1)
  • powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1
🚧 Files skipped from review as they are similar to previous changes (1)
  • powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1

📝 Walkthrough

Walkthrough

Test-MtLimitOnMicrosoftDomainUsage now uses Microsoft Defender for Office 365 licensing to select its capability path, falls back to Exchange Online only when connected, excludes discovery mailboxes, and reports mailbox lookup limitations.

Changes

Microsoft domain usage validation

Layer / File(s) Summary
License and connection path selection
powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1
The test uses Get-MtLicenseInformation -Product Mdo to select DefenderForOffice365P2, and selects ExchangeOnline only after confirming the connection.
Exchange Online mailbox filtering
powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1
The fallback excludes DiscoveryMailbox objects, matches .onmicrosoft.com primary addresses, performs Graph lookups only when an ExternalDirectoryObjectId exists, and reports unmatched display names.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: maester-test

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The Mdo/DefenderForOffice365P2 branch change and Graph lookup messaging go beyond issue #1840's mailbox-filter fix. Limit the PR to excluding DiscoveryMailbox in the Exchange Online fallback, or document the extra licensing and Graph changes in the issue.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title names the file and the licensing/mailbox filter fix, which matches the main change.
Description check ✅ Passed The description covers the fix, links issue #1840, and includes checklist items, though it omits the optional additional-information section.
Linked Issues check ✅ Passed The PR excludes DiscoveryMailbox from the Exchange Online fallback, which addresses issue #1840's failure case.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 (1)

69-72: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Optimize array assignment and guard against missing Object IDs.

Using += to append to an array inside a loop is inefficient (O(N^2)) in PowerShell and can significantly slow down the test if many mailboxes match. Additionally, checking if $mbx.ExternalDirectoryObjectId exists before invoking the Graph request will prevent the entire test from throwing a terminating error and failing entirely if a built-in or synced mailbox lacks this property.

Assign the foreach loop directly to the variable instead and cast it to an array to ensure it is always formatted correctly for downstream parameters.

⚡ Proposed refactor
-                $mgUsers = @()
-                foreach ($mbx in $mbxes) {
-                    $mgUsers += Invoke-MtGraphRequest -RelativeUri "users" -UniqueId $mbx.ExternalDirectoryObjectId
-                }
+                [array]$mgUsers = foreach ($mbx in $mbxes) {
+                    if ($mbx.ExternalDirectoryObjectId) {
+                        Invoke-MtGraphRequest -RelativeUri "users" -UniqueId $mbx.ExternalDirectoryObjectId
+                    }
+                }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1`
around lines 69 - 72, Update the $mgUsers population loop to assign its output
directly to [array]$mgUsers instead of using +=. Before calling
Invoke-MtGraphRequest, verify that $mbx.ExternalDirectoryObjectId is present;
skip mailboxes without an ID, while preserving the existing users request for
valid IDs.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1`:
- Line 64: Update the Get-Mailbox invocation in
Test-MtLimitOnMicrosoftDomainUsage to specify an unlimited result size, while
preserving the existing discovery-mailbox filter and subsequent Where-Object
filtering.

---

Nitpick comments:
In `@powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1`:
- Around line 69-72: Update the $mgUsers population loop to assign its output
directly to [array]$mgUsers instead of using +=. Before calling
Invoke-MtGraphRequest, verify that $mbx.ExternalDirectoryObjectId is present;
skip mailboxes without an ID, while preserving the existing users request for
valid IDs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 11c62e54-a811-41f4-b7bc-4d6a7ee858b5

📥 Commits

Reviewing files that changed from the base of the PR and between 17c6f8f and 42911a6.

📒 Files selected for processing (1)
  • powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1

Comment thread powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 Outdated
@merill

merill commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Thank you for looking into this and fixing it @JeanPhilippeGeorge much appreciated. I made a small tweak to fix the CodeRabbit recommendation since the old code was only checking the first 1000 and would provide a Pass false positive.

@merill merill left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@merill
merill merged commit c45c1dc into maester365:main Jul 22, 2026
12 of 13 checks passed
@JeanPhilippeGeorge

Copy link
Copy Markdown
Contributor Author

@merill, nice catch. I didn't pay attention to the filtering on the client not on the server!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🪲 MT.1074 / Test-MtLimitOnMicrosoftDomainUsage: the no-Defender fallback always fails on the built-in Discovery Search Mailbox

2 participants