Skip to content

Commit

Permalink
fix anchor issues bulk requests
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinTegelaar committed Feb 7, 2025
1 parent 3e8d41f commit aca4768
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Modules/CIPPCore/Public/GraphHelper/New-ExoBulkRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function New-ExoBulkRequest {
$OnMicrosoft = $Tenant.initialDomainName
$Anchor = "UPN:SystemMailbox{8cc370d3-822a-4ab8-a926-bb94bd0641a9}@$($OnMicrosoft)"
}
$Headers['X-AnchorMailbox'] = $Anchor
$Headers['X-AnchorMailbox'] = "APP:SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@$($tenant.customerId)"
$Headers['X-CmdletName'] = $cmd.CmdletInput.CmdletName
$Headers['Accept'] = 'application/json; odata.metadata=minimal'
$Headers['Accept-Encoding'] = 'gzip'
Expand Down
14 changes: 1 addition & 13 deletions Modules/CIPPCore/Public/GraphHelper/New-ExoRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,7 @@ function New-ExoRequest {
}
}
if (!$Anchor) {
if ($cmdparams.Identity) { $Anchor = $cmdparams.Identity }
if ($cmdparams.anr) { $Anchor = $cmdparams.anr }
if ($cmdparams.User) { $Anchor = $cmdparams.User }
if ($cmdparams.mailbox) { $Anchor = $cmdparams.mailbox }
if (!$Anchor -or $useSystemMailbox) {
if (!$Tenant.initialDomainName -or $Tenant.initialDomainName -notlike '*onmicrosoft.com*') {
$OnMicrosoft = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/domains?$top=999' -tenantid $tenantid -NoAuthCheck $NoAuthCheck | Where-Object -Property isInitial -EQ $true).id
} else {
$OnMicrosoft = $Tenant.initialDomainName
}
$anchor = "UPN:SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@$($OnMicrosoft)"
if ($cmdlet -in 'Set-AdminAuditLogConfig', 'Get-AdminAuditLogConfig', 'Enable-OrganizationCustomization', 'Get-OrganizationConfig', 'Set-OrganizationConfig') { $anchor = "UPN:SystemMailbox{8cc370d3-822a-4ab8-a926-bb94bd0641a9}@$($OnMicrosoft)" }
}
$anchor = "APP:SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@$($tenant.customerId)"
}
#if the anchor is a GUID, try looking up the user.
if ($Anchor -match '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function Invoke-CIPPStandardAutoAddProxy {
param(
$Tenant,
$Settings,
$QueueItem
)

if ($Settings.remediate -eq $true) {
$Domains = New-ExoRequest -TenantId $Tenant -Cmdlet 'Get-AcceptedDomain' | Select-Object -ExpandProperty DomainName

$AllMailboxes = New-ExoRequest -TenantId $Tenant -Cmdlet 'Get-Mailbox'
foreach ($Domain in $Domains) {
$ProcessMailboxes = $AllMailboxes | Where-Object {
$addresses = @($_.EmailAddresses) -replace '^[^:]+:' # remove SPO:, SMTP:, etc.
$hasDomain = $addresses | Where-Object { $_ -like "*@$Domain" }
if ($hasDomain) { return $false } else { return $true }
}

$bulkRequest = foreach ($Mailbox in $ProcessMailboxes) {
$LocalPart = $Mailbox.UserPrincipalName -split '@' | Select-Object -First 1
$NewAlias = "$LocalPart@$Domain"
@{
CmdletInput = @{
CmdletName = 'Set-Mailbox'
Parameters = @{Identity = $Mailbox.Identity ; EmailAddresses = @{
'@odata.type' = '#Exchange.GenericHashTable'
Add = "smtp:$NewAlias"
}
}
}
}
}
$BatchResults = New-ExoBulkRequest -tenantid $Tenant -cmdletArray @($bulkRequest)
$BatchResults | ForEach-Object {
if ($_.error) {
$ErrorMessage = Get-CippException -Exception $_.error
Write-Host "Failed to apply new email policy to $($_.target) Error: $($ErrorMessage.NormalizedError)"
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to apply Delegate Sent Items Style to $($_.error.target) Error: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
}
}
}
}
}

0 comments on commit aca4768

Please sign in to comment.