Unable to connect to exchange online

Yaser Mowlawizadah 0 Reputation points
2024-11-25T15:47:32.5566667+00:00

While using Connect-ExchangeOnline -CertificateThumbprint $CertificateThumbprint -AppId $ClientId -Organization $Organization Command to establish connection to exchange online, randomly I get the error the following error:


Module could not be correctly formed. Please run Connect-ExchangeOnline again.
At C:\Users\MY-USER\Documents\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.6.0\netFramework\ExchangeOnlineManagement.psm1:766 char:21
+                     throw $_.Exception;
+                     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RuntimeException


Everything was working fine, this started happening out of no-where, and even now it works like 7 times and fails once.

The App that I use is granted the following API Permissions:Exchange-per And also has a role named as Exchange Administrator from Microsoft Entra set of roles assigned to itself.

  • I also looked here: https://learn.microsoft.com/en-us/powershell/exchange/app-only-auth-powershell-v2?view=exchange-ps and can see that the permissions and certificate are set properly.
  • I also updated the ExchangeOnlineManagement module, no luck yet
  • This is part of a bigger playbook which runs automatically every night at 1 AM to offboard users, when it runs as a scheduled job everything works fine, but if I am to trigger the whole playbook and launch it or try this command only I get this error

Thanks for the help

Microsoft Exchange Online
Microsoft Exchange
Microsoft Exchange
Microsoft messaging and collaboration software.
650 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jake Zhang-MSFT 8,400 Reputation points Microsoft Vendor
    2024-11-26T07:10:42.7166667+00:00

    Hi @Yaser Mowlawizadah ,

    Welcome to the Microsoft Q&A platform!

    Based on your description, you're encountering an intermittent issue with the Exchange Online Management Module. Here are a few troubleshooting steps you can try to resolve this problem:

    1. Make sure you are using the latest version of the module.
    Update-Module -Name ExchangeOnlineManagement
    
    1. Sometimes, reinstalling the module can fix issues related to corrupted files.
    Uninstall-Module -Name ExchangeOnlineManagement -AllVersions 
    Install-Module -Name ExchangeOnlineManagement
    
    1. Sometimes, clearing the current PowerShell session and restarting it can help.
    Remove-Module ExchangeOnlineManagement 
    Start-Sleep -Seconds 10 
    Import-Module ExchangeOnlineManagement 
    Connect-ExchangeOnline
    
    1. Make sure you have a stable and reliable network connection as intermittent connectivity issues can sometimes cause this problem.
    2. Test by connecting from a different machine or user profile to determine if the issue is specific to your current setup.
    3. You can enable diagnostic logging to get more detailed information about what might be causing the issue.
    $DebugPreference = "Continue" 
    Connect-ExchangeOnline -UserPrincipalName <yourUPN> -ShowProgress $true -Verbose
    
    

    Please feel free to contact me for any updates. And if this helps, don't forget to mark it as an answer.

    Best,

    Jake Zhang


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.