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

Darren Rose 281 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  
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,755 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,631 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
556 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,576 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Vasil Michev 109.9K Reputation points MVP
    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 281 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,271 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.