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
There are several messages rolled up in the error you've received:
These messages help identify where to look. In the first message,
parameter 'EmailAddresses'can only refer to theSet-DistributionGroupcommand, 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
$varis built, a likely problem immediately presents itself:$varhas 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:
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):
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-DistributionGroupas the value for the parameter-EmailAddresses.An Array is exactly what's called for, and thus the correct construct to use.