I am trying to push this powershell script to migrate the On premise exchange distribution lists to 365 but running into an error I have posted a screen shot of the error and script below.

#Connect to Exchange Online
#Connect-ExchangeOnline

#Source - Provide the name of the on-prem distribution group you wish to migrate
$DistGroup = "test group"

#To avoid naming conflicts, we will append the word copy to the end of the migrate distribution group name
$Append = "copy"

#Name for the new cloud distribution group
$CloudGroup = "$DistGroup $Append"

#Import the csv file with all of the on-prem distribution groups
#Target the on-prem distribution group you wish to copy
$DistGroupCSV = Import-csv -path "\\vhall02\it\GADGroup\projects\distribution list\dlist.csv" | where-object {$_."display name" -eq $DistGroup}
$DistGroupname = $DistGroupCSV.'DISPLAY NAME'
$c = Get-DistributionGroup -identity $DistGroupname
$smtpaddresses = $c.EmailAddresses
New-DistributionGroup -Name $CloudGroup -Type "distribution"
$var = '@{Add='
foreach ($smtpaddress in $smtpaddresses){
$var = $var+ '"'+$smtpaddress+'",'
}
$var = $var.substring(0,$var.length-1)
$var = $var+ '}'
Set-DistributionGroup -Identity $CloudGroup -EmailAddresses $var
Get-DistributionGroup $CloudGroup | format-table

I tried running this script but no success

Error Message

1

There are 1 best solutions below

1
BGunnells On

There are several messages rolled up in the error you've received:

 1. Cannot process argument transformation on parameter 'EmailAddresses'.
 2. Cannot convert value [...] to type "Microsoft.Exchange.Data.ProxyAddressCollection"
 3. The address [...] is invalid: the address prefix (type) cannot exceed 9 characters.

These messages help identify where to look. In the first message, parameter 'EmailAddresses' can only refer to the Set-DistributionGroup command, as that's the only place it appears in your script. Here we see that the variable "$var" is the value reported to this parameter. The second message indicates that the value reported (i.e. $var) is not of the correct data type and cannot be converted.

Tracing back to see how $var is built, a likely problem immediately presents itself: $var has been created as a Hashtable @{}. A Hashtable, however, is an unnecessary construct in this use case; a simple Array @() is more correctly suited.

Try this:

# Replace lines 20 - 25 in your script with the following:
$var = '@('          #<- Change curly brace to parenthesis and remove "Add="
foreach ($smtpaddress in $smtpaddresses){
  $var = $var + '"' + $smtpaddress + '",'
}
$var = $var.substring(0,$var.length-1)
$var = $var + ')'    #<- Change curly brace to parenthesis again here

The reason why this is important is that an Array is a collection of individual items, whereas a Hashtable is a set of key/value pairs (See here for an excellent discussion on the differences between the two). To quote from the aforementioned linked resource (emphasis mine):

"Arrays and [Hashtables] are both data structures. They can both hold multiple values, and they can both be stored in a single variable. The difference is keys and values. Arrays hold single items in each index, while a hash table holds a key-value pair in each."

As such, in this particular case, we have no need of "key/value pairs" -- rather, we simply need a list (or "collection") of the individual SMTP addresses to pass to Set-DistributionGroup as the value for the parameter -EmailAddresses.

An Array is exactly what's called for, and thus the correct construct to use.