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

Darren Rose 281 Reputation points


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 = ""  
        Dim credential As New PSCredential(Username_String, Password_SecureString)  
        Dim connectionInfo As New WSManConnectionInfo(New Uri(connectionUri), "", credential)  
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic  
        connectionInfo.MaximumConnectionRedirectionCount = 2  
        Dim results As Collection(Of PSObject)  
            Using runspace As Runspace = RunspaceFactory.CreateRunspace(connectionInfo)  
                Using powershell As PowerShell = PowerShell.Create()  
                    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  
        End Try  
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,400 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.
3,867 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.
466 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,172 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Vasil Michev 85,401 Reputation points MVP

    The best thing to do here is switch to using certificate-based auth as detailed in this article:

  2. Darren Rose 281 Reputation points

    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.

    0 comments No comments

  3. KyleXu-MSFT 26,171 Reputation points

    @Darren Rose

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

    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