From 42911a68811baaab8a916ece9328f84c6c0bea54 Mon Sep 17 00:00:00 2001 From: Jean-Philippe George <36743032+JeanPhilippeGeorge@users.noreply.github.com> Date: Tue, 21 Jul 2026 10:05:16 +0200 Subject: [PATCH 1/3] Update Test-MtLimitOnMicrosoftDomainUsage.ps1 --- .../Test-MtLimitOnMicrosoftDomainUsage.ps1 | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 b/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 index 37a28e244..244baac8d 100644 --- a/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 +++ b/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 @@ -19,22 +19,17 @@ [OutputType([bool])] param() - - if ( ( Get-MtLicenseInformation DefenderXDR ) -ne "DefenderXDR" ) { - # Add-MtTestResultDetail -SkippedBecause NotLicensedDefenderXDR - # return $null - if (-not (Test-MtConnection ExchangeOnline)) { - Add-MtTestResultDetail -SkippedBecause NotConnectedExchange - return $null - } else { - $checkType = "ExchangeOnline" - } + if (Get-MtLicenseInformation -Product Mdo) { + $checkType = "DefenderForOffice365P2" + } elseif (-not (Test-MtConnection ExchangeOnline)) { + Add-MtTestResultDetail -SkippedBecause NotConnectedExchange + return $null } else { - $checkType = "DefenderXDR" + $checkType = "ExchangeOnline" } $return = $true - if ($checkType -eq "DefenderXDR") { + if ($checkType -eq "DefenderForOffice365P2") { Write-Verbose "Checking if mailboxes send outbound mails using the .onmicrosoft.com domain..." try { $outboundTreshold = 100 @@ -66,16 +61,16 @@ } elseif ($checkType -eq "ExchangeOnline") { Write-Verbose "Checking if mailboxes use the .onmicrosoft.com domain as primary SMTP address..." try { - $allMbx = Get-Mailbox | Where-Object { $_.PrimarySmtpAddress -like "*@*.onmicrosoft.com" } - if (($allMbx | Measure-Object).Count -eq 0) { + $mbxes = Get-Mailbox -Filter "RecipientTypeDetails -ne 'DiscoveryMailbox'" | Where-Object { $_.PrimarySmtpAddress -like "*@*.onmicrosoft.com" } + if (($mbxes | Measure-Object).Count -eq 0) { $result = "Well done. No mailbox uses the .onmicrosoft.com domain as primary SMTP address." Add-MtTestResultDetail -Result $result } else { $mgUsers = @() - foreach ($mbx in $allMbx) { + foreach ($mbx in $mbxes) { $mgUsers += Invoke-MtGraphRequest -RelativeUri "users" -UniqueId $mbx.ExternalDirectoryObjectId } - $result = "Your tenant has $(($allMbx | Measure-Object).Count) mailboxes using the .onmicrosoft.com domain as primary SMTP address:`n`n%TestResult%" + $result = "Your tenant has $(($mbxes | Measure-Object).Count) mailboxes using the .onmicrosoft.com domain as primary SMTP address:`n`n%TestResult%" $return = $false Add-MtTestResultDetail -Result $result -GraphObjects $mgUsers -GraphObjectType Users } From ea4dfaabf08ffba07eb59a789ab3eec8309e1e04 Mon Sep 17 00:00:00 2001 From: Jean-Philippe George <36743032+JeanPhilippeGeorge@users.noreply.github.com> Date: Wed, 22 Jul 2026 12:43:44 +0200 Subject: [PATCH 2/3] Implemented bot suggestion + Improve foreach / array + Handle mailbox without ExternalDirectoryObjectId --- .../exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 b/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 index 244baac8d..ee702f255 100644 --- a/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 +++ b/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 @@ -67,10 +67,19 @@ Add-MtTestResultDetail -Result $result } else { $mgUsers = @() - foreach ($mbx in $mbxes) { - $mgUsers += Invoke-MtGraphRequest -RelativeUri "users" -UniqueId $mbx.ExternalDirectoryObjectId + $mailboxWithoutExternalDirectoryObjectIdDisplayNames = $mbxes | ` + Where-Object { -not $_.ExternalDirectoryObjectId } | ` + Select-Object -ExpandProperty DisplayName + [array]$mgUsers = foreach ($mbx in $mbxes) { + if ($mbx.ExternalDirectoryObjectId) { + Invoke-MtGraphRequest -RelativeUri "users" -UniqueId $mbx.ExternalDirectoryObjectId + } } $result = "Your tenant has $(($mbxes | Measure-Object).Count) mailboxes using the .onmicrosoft.com domain as primary SMTP address:`n`n%TestResult%" + if (($mailboxWithoutExternalDirectoryObjectIdDisplayNames | Measure-Object).Count -ge 1) { + $mailboxWithoutExternalDirectoryObjectIdDisplayNamesResult = $mailboxWithoutExternalDirectoryObjectIdDisplayNames -join "`n+ " + $result += "`n`nThe following mailboxes have no ExternalDirectoryObjectId and could not be looked up in Microsoft Graph:`n+ $mailboxWithoutExternalDirectoryObjectIdDisplayNamesResult" + } $return = $false Add-MtTestResultDetail -Result $result -GraphObjects $mgUsers -GraphObjectType Users } From 830a3644627ff86f9c36c0d2f89d7d475ee14ea7 Mon Sep 17 00:00:00 2001 From: Merill Fernando Date: Wed, 22 Jul 2026 23:26:41 +1000 Subject: [PATCH 3/3] Fetch all mailboxes for domain usage check --- .../maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 b/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 index ee702f255..03a1e8448 100644 --- a/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 +++ b/powershell/public/maester/exchange/Test-MtLimitOnMicrosoftDomainUsage.ps1 @@ -61,7 +61,7 @@ } elseif ($checkType -eq "ExchangeOnline") { Write-Verbose "Checking if mailboxes use the .onmicrosoft.com domain as primary SMTP address..." try { - $mbxes = Get-Mailbox -Filter "RecipientTypeDetails -ne 'DiscoveryMailbox'" | Where-Object { $_.PrimarySmtpAddress -like "*@*.onmicrosoft.com" } + $mbxes = Get-Mailbox -ResultSize Unlimited -Filter "RecipientTypeDetails -ne 'DiscoveryMailbox'" | Where-Object { $_.PrimarySmtpAddress -like "*@*.onmicrosoft.com" } if (($mbxes | Measure-Object).Count -eq 0) { $result = "Well done. No mailbox uses the .onmicrosoft.com domain as primary SMTP address." Add-MtTestResultDetail -Result $result