Search code examples
powershellexchange-serverpowershell-remoting

Powershell - Exchange - string as multiple parameters - Set-SendConnector


i have a problem with powershell and "how to pass multiple parameters" to one Exchange Powershell Comandlet.

We have a Exchangeserver with multiple SendConnectors. Each SendConnector has around 1000 Mail-Domains. https://learn.microsoft.com/de-de/powershell/module/exchange/set-sendconnector?view=exchange-ps

Lets say I have a variable:

$mailDomains = '"mail1.de","mail2.de","mail3.de"';

If I type the command "by hand":

Set-SendConnector -Identity "MyConnectorName001" -AddressSpaces "mail1.de","mail2.de","mail3.de"

That is working. But if I want to do it like this:

Set-SendConnector -Identity "MyConnectorName001" -AddressSpaces $mailDomains

It fails, because it is reading the variable as one String

Is there a way to provide -AddressSpacecs multiple domains with a variable?

I tried to split the string variable $mailDomains into an array:

$myArray = $mailDomains.Split(',');
Set-SendConnector -Identity "MyConnectorName001" -AddressSpaces $myArray

no success because "System.Collections.Hashtable" can not be converted to "Microsoft.Exchange.Data.MultiValuedProperty`1[Microsoft.Exchange.Data.AddressSpace]"

I tried to iterate the array with the following

foreach($mailDomain in $mailDomains)
{
    Set-SendConnector -Identity "MyConnectorName001" -AddressSpaces @{add=$mailDomain}
}

That is working but it takes 10 minutes until all mailDomains are added in one Connector. When I use the Set-SendConnector command with 1000 Addresses in one single command it takes 1 - 2 seconds for each connector.


Solution

  • You mention you've tried to split $maildomains into an array, but you don't include any info on how you did that.

    The syntax I think you're looking for would be :

    $mailDomains = @("mail1.de","mail2.de","mail3.de")

    so the entire thing is enclosed within @() and without the single quotes on each end that you had.

    Edit. Thanks for clarifying about the array. I wonder if [Microsoft.Exchange.Data.AddressSpace] is taking issue with the quotes around those domains.

    If you try running the following

    $mailDomains = '"mail1.de","mail2.de","mail3.de"';
    $myArray = $mailDomains.Split(',');
    $myArray
    $myArray.GetType()
    
    $mailDomains2 = "mail1.de","mail2.de","mail3.de";
    $myArray2 = $mailDomains2.Split(',');
    $myArray2
    $myArray2.GetType()
    
    $mailDomains3 = @("mail1.de","mail2.de","mail3.de")
    $mailDomains3
    $mailDomains3.GetType()
    

    The output that is returned is :

    "mail1.de"
    "mail2.de"
    "mail3.de"
    
    IsPublic IsSerial Name                                     BaseType                                                                                                                                                                  
    -------- -------- ----                                     --------                                                                                                                                                                  
    True     True     String[]                                 System.Array                                                                                                                                                              
    mail1.de
    mail2.de
    mail3.de
    True     True     Object[]                                 System.Array                                                                                                                                                              
    mail1.de
    mail2.de
    mail3.de
    True     True     Object[]                                 System.Array 
    

    Which shows that all three variations on how to add them to an array successfully create an array, but your current method with the array values surrounded with single quotes results in the values being passed on retaining their double quotes.