Justin Durlewanger February 2016

convert System.String to MultiValuedProperty

I am trying to create a user execution script to prompt for users and feed them to another command using a variable. Something like this:

$owners  = Read-Host
$members = Read-Host

New-DistributionGroup -Name test -ManagedBy $owners -Members $members

The problem is that Read-Host provides a typename of System.String and the -ManagedBy and -Members switches require a MultiValuedProperty. I've tried doing an export of the System.String to CSV, but it doesn't break up the multiple values even if they are separated by a comma.

I know if I just populate a CSV and do an import it will work but I am distributing this script to branch offices and I need them to have as few steps as possible.

Answers


Alexis Coles February 2016

So if the string they provide is a comma delimited string you can do something like.

$owners = (Read-host).Split(',')

Much the same for members...


Ansgar Wiechers February 2016

"Multi-valued property" just means that you can provide multiple values (i.e. an array) as the argument to the parameter. If your users input something like

alice@example.com,bob@example.com,charlie@example.com

you need to split the (comma-separated) string to get an actual array:

$owners  = $owners -split ' *, *'
$members = $members -split ' *, *'

The expression *, * will also take care of spaces that the user may have put before and/or after the separating commas.

Post Status

Asked in February 2016
Viewed 2,125 times
Voted 11
Answered 2 times

Search




Leave an answer