I trying to do bulk change of distribution lists, using Powershell script, but didn't made it far.
I importing new addresses and aliases from a .CSV file and seems like this is the issue. Because when I assigning an address and an alias to an exact DL, then it all fine. Or maybe the script written bad.
Error message:
Cannot process argument transformation on parameter 'EmailAddresses'. Cannot convert value "System.Collections.ArrayList" to type "Microsoft.Exchange.Data.ProxyAddressCollection". Error: "The address 'SMTP:@{DisplayName=TEAM_A; PrimarySmtpAddress=a_dl@company.com; Alias=a_dl@company.org}.PrimarySmtpAddress' is invalid: The address '@{DisplayName=TEAM_A; PrimarySmtpAddress=a_dl@company.com; Alias=a_dl@company.org}.PrimarySmtpAddress' is not a valid SMTP address. Parameter name: address Actual value was @{DisplayName=TEAM_A; PrimarySmtpAddress=a_dl@company.com; Alias=a_dl@company.org}.PrimarySmtpAddress." + CategoryInfo : InvalidData: (:) [Set-DistributionGroup], ParameterBindin...mationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-DistributionGroup + PSComputerName : outlook.office365.com*
The script:
Connect-ExchangeOnline
$psmtpa = Import-Csv C:\_temp\Excange\PrimarySMTPAddress.csv
ForEach ($name in $psmtpa){
ForEach ($email in $psmtpa){
ForEach ($alias in $psmtpa){
Set-DistributionGroup "$name.DisplayName" -EmailAddresses SMTP:$email.PrimarySmtpAddress,$alias.Alias
}
}
}
And the .CSV look like this:
PS And yeah, I trying to perform it, using PowerShell ISE, instead of writing a one-liner in PowerShell app. Also, I tried WindowsEmailAddress, but powershell don't understand it what is it.
There are a few things to note, first, the double quotes on "$name.DisplayName"
are stringifying the object $name
and appending .DisplayName
to it. Remove them.
$object = [pscustomobject]@{
foo = 'var'
}
"$object.foo" # => @{foo=var}.foo
$object.foo # => 'var'
Second thing to note, on:
-EmailAddresses SMTP:$email.PrimarySmtpAddress,$alias.Alias
If you're looking to add a Primary and Secondary SMTP address, as Mathias pointed out, you should be using:
-EmailAddresses "SMTP:$($email.PrimarySmtpAddress)", "smtp:$($email.Alias)"
If instead, $email.Alias
is actually an Alias, then you should be using:
-EmailAddresses "SMTP:$($email.PrimarySmtpAddress)" -Alias $email.Alias
See Subexpression operator $( )
for more info.
Lastly, you don't need 3 loops, 1 should be enough to loop over your CSV:
$psmtpa = Import-Csv C:\_temp\Excange\PrimarySMTPAddress.csv
foreach ($object in $psmtpa) {
Set-DistributionGroup $object.DisplayName -EmailAddresses "SMTP:$($object.PrimarySmtpAddress)", "smtp:$($object.Alias)"
}