0

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:[email protected]", "smtp:[email protected]", "smtp:[email protected]"

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:[email protected] 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

5
  • Remove the " double-quotes around $using:smpts Commented May 23, 2020 at 10:32
  • it will throw an error: Invalid type 'System.Management.Automation.PSObject'. Parameter name: proxyaddresses + CategoryInfo : InvalidArgument: (OldIT:ADGroup) [Set-ADGroup], ArgumentException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.SetADGroup + PSComputerName : dc01 Commented May 23, 2020 at 10:39
  • That's a problem with the object referenced by $using:ListBox1.SelectedItem, maybe try $GroupName = $ListBox1.SelectedItem.DistinguishedName and then pass $using:GroupName to Set-ADGroup inside the remote script block Commented May 23, 2020 at 10:41
  • that could work i guess but i need to change more code lines for that cuz $listbox1.selecteditems are just strings when we get here, is there no way to pass array of strings and reach each of them for itself? im guessing a solution would be a loop going inside the array and invoking command for every item but thats like an overkill ithink Commented May 23, 2020 at 10:52
  • 2
    Try -Add @{proxyaddresses= [string[]]$using:smtps} Commented May 23, 2020 at 13:28

1 Answer 1

3

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}
Sign up to request clarification or add additional context in comments.

1 Comment

Or when he sets smtps, he should set it with the [string[]] tag...Powershell is a weakly typed language, meaning it will attempt to detect the type of variable you are setting, (as opposed to strongly typed). Sometimes it get's it wrong, so identifying it yourself can remove any possible errors that might arise..Because his example starts with @smtps = "bla" the @smtps = might be confusing powershell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.