Multivalued Parameters in PowerShell (RemoteIPRanges in Set-ReceiveConnector)

A number of parameters for EMS (the Exchange Management Shell - which is just PowerShell v1 plus the Exchange snap-ins) require the specification of a multivalued property.

A multivalued property is just what it sounds like - a property that has multiple values.

A very common one is the RemoteIPRanges parameter for Set-ReceiveConnector.

It is common to see people asking how to specify multiple IP addresses and multiple IP ranges as a parameter to the cmdlet. It's actually pretty easy, once you know the trick.

Let's get some background. As you may know (or may not - just trust me here), PowerShell uses the comma character (",") as an operator - not as a simple argument separator. In many languages, this statement:

	callsub "a", 1, "hi"

would mean to call the subroutine named "callsub" with three parameters. But not in PowerShell! For PowerShell, this means to call the subroutine named "callsub" with ONE parameter - an array of three objects. This is easier to see by doing this:

	$a = "a", 1, "hi"
	$a.GetType()

and the result may surprise you:

	IsPublic IsSerial Name                                     BaseType
	-------- -------- ----                                     --------
	True     True     Object[]                                 System.Array

To get the "other" behavior in PowerShell, you eliminate the commas:

	callsub "a" 1 "hi"

For me, this was one of the most difficult things to adjust to in PowerShell. But now that we are armed with that knowledge, it is pretty obvious how we get our desired behavior in with the RemoteIPRanges parameter - we separate each argument with a comma. For example:

	Set-ReceiveConnector Connector1 -RemoteIPRanges ("192.168.1.1-192.168.1.10", "192.168.1.20", "192.168.1.254")

The parenthesis are just used to making the grouping clear. Now that we know that, once we've configured a ReceiveConnector, how do we update it without typing in all of those IP addresses again? We use PowerShell object references! This is a little tricky, but I bet it'll make sense when you see it.

	$a = Get-ReceiveConnector Connector1
	$a.RemoteIPRanges += "192.168.2.20"
	$a | Set-ReceiveConnector

Pretty cool, huh!?

Until next time...

If there are things you would like to see written about, please let me know!

Published Friday, February 08, 2008 9:44 AM by michael
Filed under: , ,

Comments

Tuesday, April 07, 2009 2:54 PM by Michael's meanderings...

# More Multi-valued Parameters in PowerShell (SourceTransportServers in Set-SendConnector)

In my first article on Multi-valued Parameters in PowerShell , I discussed a certain class of array parameters