Adding External Email addresses to a new distribution group

Welcome Forums General PowerShell Q&A Adding External Email addresses to a new distribution group

Viewing 7 reply threads
  • Author
    Posts
    • #247131
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      HI, I’m pretty new to powershell and I have had Microsoft guys on the phone and they dont seem to be able to do what I need to achieve.

      I have a CSV file with 580 email addresses and I want to bulk import them to my new group. Initially I was told that we would need to import them into contacts and from there, we would import them to the new group.

      Then on another call,  I was told that  -no -we could import them straight into the new group.  So I spent the next 3 phone calls with Microsoft running scripts that didnt work and one that partially worked.

      Here’s the script I ran last:  Import-Csv C:\Users\test\Desktop\adultwellbeing.csv | foreach{Add-DistributionGroupMember -Identity AdultHealthAndWellBeing -Member $_.alias} 

      Here is the repeated error I get for each email address (email address varies in each line) and the error is repeated over and over again. I am assuming its looking at each of the addresses in the csv but not sure why its not finding what it needs – is it because they need to be in contacts first? Any info would be appreciated. Thanks.

      Couldn’t find object “[email protected]”. Please make sure that it was spelled correctly or specify a different object.

       

    • #247143
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Paul, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “CODE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      I am assuming its looking at each of the addresses in the csv but not sure why its not finding what it needs – is it because they need to be in contacts first?

      Assuming is a quite unprofessional way of solving problems. 😉

      You should always read the help for the cmdlets you’re about to use. Read it completely including the examples to learn how to use it.

      Add-DistributionGroupMember

      For the parameter -Member you can read:

      The Member parameter specifies the recipient that you want to add to the group. A member can be any mail-enabled recipient in your organization. You can use any value that uniquely identifies the recipient. For example:

      Name
      Alias
      Distinguished name (DN)
      Canonical DN
      Email address
      GUID

    • #247170
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      HI, Thanks for the welcome and the advice.

      So, on the basis that the member parameter relates to any mail enabled user within the organization,  could this possibly be the reason for the failure as the csv file contains all external (non organizational) addresses. The email addresses wont exist in the org.

      Do they have to be added into contacts first or is there a mechanism by which I can import them directly to a new but empty distribution group?

       

      Thanks

       

       

       

       

    • #247179
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      … could this possibly be the reason for the failure as the csv file contains all external (non organizational) addresses.

      … yes 😉

      Do they have to be added into contacts first

      … yes 😉

      or is there a mechanism by which I can import them directly to a new but empty distribution group?

      … not that I’m aware of.

    • #247182
      Participant
      Topics: 0
      Replies: 3
      Points: 61
      Rank: Member

      When it comes to adding external addresses to a Distribution Group, you have always needed to create contacts in Exchange in order to do this.
      Fortunately, it is relatively straightforward to accomplish using PowerShell and something that I have done a number of times when working at an MSP.
      Allow me to explain.

      The contacts need to be created so that the external addresses appear in the Address Book. Once you have created contacts, these can be added to a distribution group in the same way you add your internal domain accounts.

      The CSV file used in this example contains one column with a header of “EmailAddress”. The following code should perform the tasks you require. I have left the option -Whatif in the code so that this can be run without making any changes to your system. It will however, show you what the outcome would be without it. It is necessary to iterate through your CSV processing each email address in turn, which is achieved using the Foreach loop.

      Hope that helps in some way.

    • #247234
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      Thank You for the responses. I created a csv file with a single column  “EmailAddresses” populated with the 580 addresses I need to import. However, I got the following error when running the script on the first and consecutive iterations… I stopped the script and copied the first part:

      The external e-mail address @{[email protected]}.EmailAddress is not an SMTP e-mail address.
      + CategoryInfo : NotSpecified: (EURPR04A002.pro…m}.EmailAddress:ADObjectId) [New-MailContact], Recipi
      entTaskException
      + FullyQualifiedErrorId : [Server=DB3PR0402MB3817,RequestId=71ec952f-a935-4273-b20a-5f907ca639fe,TimeStamp=05/08
      /2020 13:48:38] [FailureCategory=Cmdlet-RecipientTaskException] C86AE5F2,Microsoft.Exchange.Management.Recipient
      Tasks.NewMailContact
      + PSComputerName : outlook.office365.com

      Couldn’t find object “@{[email protected]}.EmailAddress”. Please make sure that it was spelled
      correctly or specify a different object.
      + CategoryInfo : NotSpecified: (:) [Add-DistributionGroupMember], ManagementObjectNotFoundException
      + FullyQualifiedErrorId : [Server=DB3PR0402MB3817,RequestId=9ddea277-eb5d-48a7-994f-18a2d10fd96f,TimeStamp=05/08
      /2020 13:48:39] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] F3967A20,Microsoft.Exchange.Managemen
      t.RecipientTasks.AddDistributionGroupMember
      + PSComputerName : outlook.office365.com

      The operation couldn’t be performed because object ‘@{[email protected]}.EmailAddress’ couldn’t be
      found on ‘VI1PR04A002DC01.EURPR04A002.prod.outlook.com’.
      + CategoryInfo : NotSpecified: (:) [Set-MailContact], ManagementObjectNotFoundException
      + FullyQualifiedErrorId : [Server=DB3PR0402MB3817,RequestId=f9a50a8f-999d-4d61-8bae-45dc93b899d7,TimeStamp=05/08
      /2020 13:48:41] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 3385B06A,Microsoft.Exchange.Managemen
      t.RecipientTasks.SetMailContact
      + PSComputerName : outlook.office365.com

    • #247261
      Participant
      Topics: 0
      Replies: 81
      Points: 362
      Helping Hand
      Rank: Contributor

      The syntax "$Contact.EmailAddress" is not correct. It should be just $Contact.EmailAddress in your code.

      Surrounding a PowerShell custom object in double quotes triggers string expansion. When that occurs, only the variable itself is substituted with its value and then the remaining characters not part of the name are treated as a string. You can see in the error that the custom object performed a  ToString() and then just added the string  .EmailAddress to it.

      If you must have double quotes, then a way around it is to use the sub-expression operator  $(). Anything inside is treated as an expression by the parser. So the more verbose  "$($Contact.EmailAddress)"would technically work.

    • #247279
      Participant
      Topics: 0
      Replies: 3
      Points: 61
      Rank: Member

      AdminOfThings45 is absolutely right. Apologies for the miss-type, I clearly wasn’t paying attention when I wrote my reply.

      Hopefully that hasn’t been too confusing.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.