Search code examples
powershellcommandinvoke

Adding items in an array of strings each one at a time


I have this line of code:

invoke-command -Session $s -scriptblock {Set-Adgroup  $using:ListBox1.SelectedItem -add @{proxyaddresses="$using:smtps"}}

$s is legitimate session, $listbox.selecteditem is for example a dist group called Old-Sales-Users , and $smtps is a string array like so: @smtps = "smtp:bla@bla.bla", "smtp:bla2@bla.bla", "smtp:bla3@bla.bla"

i want to invoke this command so i can add those smtps to the proxyaddresses of the dist group. but the way this works here is that its adding the 3 strings to the same line so i get one line proxyaddress with "smtp:bla@bla.bla smtp:bla2@bla...." i want it to create 3 seperate lines (or more if there's more in that array) meaning like an ENTER is pressed after each item in the array... my second question is if thats the correct way to be doing this? because i actually open 3 invoke commands is there a way using 1 invoke command to add all that array to the proxyaddress?

Thank you


Solution

  • To post my comment as answer:

    Although this is not clear in the documentation for Set-ADUser, adding items to the ProxyAddresses list needs the array of new smtp addresses to be strongly typed, so every item in the array is of type [string]

    This means the array to add needs to be casted with [string[]]

    You can see the difference like this:

    $arr1 = 'an','array','can','also','contain','numbers',1,2,3
    $arr1.GetType().FullName  # --> System.Object[]
    
    [string[]]$arr2 = 'an','array','can','also','contain','numbers',1,2,3
    $arr2.GetType().FullName  # --> System.String[]
    

    In your case, use

    -Add @{proxyaddresses= [string[]]$using:smtps}