Add / Delete users in distribution list

JadeDASILVALIMA-7638 1 Reputation point
2022-12-15T11:17:08.397+00:00

Hello,

We used to add (and delete) users in distribution list with Exchange Online PowerShell through a basic authentication. However, Microsoft is not supporting anymore basic authentication and so our code stopped to work.

You can find below code that we were using to connect to Exchange Online PowerShell and change distribution list :

private void openConnectionToExchangeOnline()  
{  
     //1- create credential  
     PSCredential credential = new PSCredential(_username, Utils.convertToSecureString(_password));  
  
      WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(connectionUri), shellUri, credential);  
      connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;  
  
      //2- open runspace  
      runspaceExchangeOnline = RunspaceFactory.CreateRunspace(connectionInfo);  
      runspaceExchangeOnline.Open();  
  
       //3- init powershell  
      powershellExchangeOnline = PowerShell.Create();  
      powershellExchangeOnline.Runspace = runspaceExchangeOnline;  
}  
  
  
public void AddToDistributionGroup(UserMailO365 user)  
{  
        PSCommand command = new PSCommand();  
        command.AddCommand("Add-DistributionGroupMember");  
        command.AddParameter("Identity", distributionGroupName);  
        command.AddParameter("Member", user.MailMeritis);  
       _powerShellO365Command.invokeExchangeOnlineCommand(command);  
}  

We tried different options, like with modern auth, but so far nothing is working. Does someone met the same issue ? How do you succedd to add or delete users in distribution lists programmatically (C# if possible) ?

Thank you very much

Exchange Exchange Server Development
0 comments No comments
{count} votes

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.