Connecting to Exchange Online using PowerShell in VB.NET - changes since Basic Authentication retired

Darren Rose 311 Reputation points
2022-10-20T10:25:04.637+00:00

Hi

Previously used code such as below to connect and query Exchange Online - since basic authentication retirement this now can't be used - can someone please give me some pointers on how I can modify this to work with the new modern authentication requirements?

Thank you

 Dim connectionUri As String = "https://outlook.office365.com/powershell-liveid/"  
  
        Dim credential As New PSCredential(Username_String, Password_SecureString)  
  
        Dim connectionInfo As New WSManConnectionInfo(New Uri(connectionUri), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential)  
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic  
        connectionInfo.MaximumConnectionRedirectionCount = 2  
  
        Dim results As Collection(Of PSObject)  
  
        Try  
            Using runspace As Runspace = RunspaceFactory.CreateRunspace(connectionInfo)  
                Using powershell As PowerShell = PowerShell.Create()  
                    runspace.Open()  
                    powershell.Runspace = runspace  
                    powershell.AddCommand("Get-Mailbox").AddParameter("Identity", MailboxIdentityToCheck)   
                    results = powershell.Invoke()  
                End Using  
  
            End Using  
        Catch ex As Remoting.PSRemotingTransportException  
            Append_tb(TextBox1, "Credentials stored are invalid" & vbCrLf & vbCrLf & "Click Change Credentials, enter correct credentials and then click Test Connection again")  
            Exit Sub  
        Catch ex As Exception  
            Console.WriteLine(ex.Message)  
        End Try  
Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,171 questions
Exchange Exchange Server Development
Windows for business Windows Server User experience PowerShell
Developer technologies VB
{count} votes

3 answers

Sort by: Most helpful
  1. Vasil Michev 119.5K Reputation points MVP Volunteer Moderator
    2022-10-20T10:30:33.277+00:00

    The best thing to do here is switch to using certificate-based auth as detailed in this article: https://learn.microsoft.com/en-us/powershell/exchange/app-only-auth-powershell-v2?view=exchange-ps


  2. Darren Rose 311 Reputation points
    2022-10-20T19:38:36.747+00:00

    Found useful C# sample here which I converted to VB and got working both to connect using method suggested by michev and to connect by supplying username and password - so have two methods now working using modern authentication and my app is up and running again.

    https://stackoverflow.com/questions/71784345/connecting-to-exchange-online-with-a-certificate-and-c-sharp

    0 comments No comments

  3. KyleXu-MSFT 26,396 Reputation points
    2022-10-21T01:58:14.157+00:00

    @Darren Rose

    Before that you could follow this blog to enable basic for remote PowerShell temporarily (not sure whether it could work for VB):
    252802-1.png

    Due to connect to Exchange online with VB related to the development, I will help you add the “office-exchange-server-dev” tag to this thread.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

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.