How to add user(s) to distribution list group in Office 365 using C#?

Welcome Forums General PowerShell Q&A How to add user(s) to distribution list group in Office 365 using C#?

Viewing 1 reply thread
  • Author
    Posts
    • #185507
      Participant
      Topics: 1
      Replies: 0
      Points: 16
      Rank: Member

      I tried below code to add user to distribution list . But command fails to me. Any suggestions?

      < !–more–>

      string connectionUri = "< a href="https://outlook.office365.com/powershell-liveid/" target="_blank">https://outlook.office365.com/powershell-liveid/< /a>" target="_blank">https://outlook.office365.com/powershell-liveid/< /a" target="_blank">https://outlook.office365.com/powershell-liveid/>";
      string loginPassword = ConfigurationManager.AppSettings["PWD"].ToString();
      string loginUser = ConfigurationManager.AppSettings["UID"].ToString();
      SecureString secpassword = new SecureString();
      foreach (char c in loginPassword)
      {
      secpassword.AppendChar(c);
      }
      PSCredential credential = new PSCredential(loginUser, secpassword);
      Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
      PowerShell powershell = PowerShell.Create();
      PSCommand command = new PSCommand();
      command.AddCommand("New-PSSession");
      command.AddParameter("ConfigurationName", "Microsoft.Exchange");
      command.AddParameter("ConnectionUri", new Uri(connectionUri));
      command.AddParameter("Credential", credential);
      command.AddParameter("Authentication", "Basic");
      powershell.Commands = command;
      
      runspace.Open();
      powershell.Runspace = runspace;
      Collection< System.Management.Automation.PSObject> result = powershell.Invoke();
      if (powershell.Streams.Error.Count > 0 || result.Count != 1)
      {
      throw new Exception("Fail to establish the connection");
      }
      
      powershell = PowerShell.Create();
      command = new PSCommand();
      command.AddCommand("Invoke-Command");
      string DLGroupName = "UnsubscribersList";
      command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create("Add-DistributionGroupMember -Identity " + DLGroupName + " -Member " + txtEmail.Text));
      command.AddParameter("Session", result[0]);
      powershell.Commands = command;
      powershell.Runspace = runspace;
      result = powershell.Invoke();
      if (powershell.Streams.Error.Count > 0 || result.Count != 1)
      {
      throw new Exception("Command failed");
      }

      I am getting this error while runningĀ Add-DistributionGroupMemberĀ command

      < !–more–>

      {Couldn't find object "test11@gmail.com". Please make sure that it was spelled correctly or specify a different object.}

      UPDATE:

      I tried add email address to contacts then adding to distribution List still get an error.

       using (powershell = PowerShell.Create())
                  {
                      command = new PSCommand();
                      command.AddCommand("Invoke-Command");
      
                      command.AddParameter("ScriptBlock",
                          System.Management.Automation.ScriptBlock.Create(
                              "New-MailContact -Name '" + txtEmail.Text + "' -ExternalEmailAddress '" + txtEmail.Text + "'"));
                      command.AddParameter("Session", result[0]);
                      powershell.Commands = command;
                      powershell.Runspace = runspace;
                      result = powershell.Invoke();
                      if (powershell.Streams.Error.Count > 0 || result.Count != 1)
                      {
                          throw new Exception("Fail to establish the connection");
                      }
                  }
      
                  using (powershell = PowerShell.Create())
                  {
                      command = new PSCommand();
                      command.AddCommand("Invoke-Command");
                      string DLGroupName = "UnsubscribersList";
                      command.AddParameter("ScriptBlock",
                          System.Management.Automation.ScriptBlock.Create(
                              "Add-DistributionGroupMember -Identity '" + DLGroupName + "' -Member " + txtEmail.Text + ""));
                      command.AddParameter("Session", result[0]);
                      powershell.Commands = command;
                      powershell.Runspace = runspace;
                      result = powershell.Invoke();
                      if (powershell.Streams.Error.Count > 0 || result.Count != 1)
                      {
                          throw new Exception("Fail to establish the connection");
                      }
                  }

      Error

      Cannot convert the "test11@gmail.com" value of type "Deserialized.Microsoft.Exchange.Data.Directory.Management.MailContact" to 
       type "System.Management.Automation.Runspaces.PSSession".
    • #185750
      Senior Moderator
      Topics: 8
      Replies: 1141
      Points: 3,927
      Helping Hand
      Rank: Community Hero

      you are using result[0] as an argument for -Session, but the error says that its not a valid session object. Make sure it is a session object before consuming it.

      System.Management.Automation.Runspaces.PSSession result = powershell.Invoke();
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.