microsoft / microsoft/CSS-Exchange

Adopt shared Connect-EXOAdvanced in PublicFolders/MailPublicFolderSync/* scripts

Open
#2,592 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Public Folders Triage Work - Medium Change
Dominant language
PowerShell
Stars
1.3k
Forks
395
Avg merge
14h 7m
Merged PRs (30d)
5

Description

Parent: #2590
Depends on: #2591

Refactor the five root scripts in PublicFolders/MailPublicFolderSync/ to use the shared Connect-EXOAdvanced / Disconnect-EXOAdvanced helper. All five carry the same silent-failure pattern documented in the parent issue.

Scripts

  • PublicFolders/MailPublicFolderSync/Sync-ModernMailPublicFolders.ps1 — remove the connect block PR #2589 just added and call the helper instead. Preserves sovereign-cloud support.
  • PublicFolders/MailPublicFolderSync/Sync-MailPublicFolders.ps1
  • PublicFolders/MailPublicFolderSync/Sync-MailPublicFoldersCloudToOnprem.ps1
  • PublicFolders/MailPublicFolderSync/Import-MailPublicFolders.ps1also fix unguarded Disconnect-ExchangeOnline at line 244 and line 257 — replace with Disconnect-EXOAdvanced -Connection $exo so we only tear down what we opened.
  • PublicFolders/MailPublicFolderSync/Import-PublicFolderMailboxes.ps1

Problem code (representative)

Sync-MailPublicFolders.ps1#L169-L193:

try {
    Import-Module ExchangeOnlineManagement -ErrorAction SilentlyContinue
    if (Get-Module ExchangeOnlineManagement) {
        $connectParams = @{
            ConnectionUri = $ConnectionUri
            Prefix        = "Remote"
            ErrorAction   = "SilentlyContinue"      # 🐛 silent auth failure
        }
        if ($null -ne $Credential) { $connectParams.Credential = $Credential }
        Connect-ExchangeOnline @connectParams
        $script:isConnectedToExchangeOnline = $true # 🐛 set even if Connect failed
    } else {
        Write-Warning $LocalizedStrings.EXOV2ModuleNotInstalled
        exit
    }
} finally { ... }

Target pseudocode

. $PSScriptRoot\..\..\Shared\M365\EXOConnection.ps1

$exo = Connect-EXOAdvanced -ConnectionUri $ConnectionUri `
                           -AzureADAuthorizationEndpointUri $AzureADAuthorizationEndpointUri `
                           -Credential $Credential `
                           -Prefix 'Remote'
if (-not $exo) { return }   # helper printed the reason

try {
    # ...existing sync logic...
} finally {
    Disconnect-EXOAdvanced -Connection $exo   # only disconnects the session we opened
}

Removes:

  • The InitializeExchangeOnlineRemoteSession function
  • The $script:isConnectedToExchangeOnline state variable
  • The Disconnect-ExchangeOnline cleanup block
  • The EXOV2ModuleNotInstalled localized string (owned by the helper now)

Validation

  • Existing Pester tests for Sync-ModernMailPublicFolders.ps1 must be updated to mock Connect-EXOAdvanced / Disconnect-EXOAdvanced instead of Connect-ExchangeOnline / Import-Module.
  • New Pester tests for the other four scripts covering: connect success, connect failure returns early, disconnect only called when connect succeeded.
  • Lab test: minimum one full sync run in a real tenant and one in a sovereign-cloud tenant.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Shared/M365/EXOConnection.ps1 and the five scripts under PublicFolders/MailPublicFolderSync/, comparing their current connection and cleanup blocks with the target helper usage. Update the existing Sync-ModernMailPublicFolders.ps1 Pester tests and add coverage for the other four scripts for connection success, early return on failure, and conditional disconnect; done includes the requested full-sync lab checks in standard and sovereign-cloud tenants.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
cloud, tooling
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.