Concatenating Variables

I’m trying to make a variable from a number of the variables, so that I can create a global address list in Exchange. Here’s a list of the variables I’m using:

$CompanyName=“Test 1”
$ADGroupType1=“Broadcast Group”
$MSPMailTargetOU=(“OU=” + $CompanyName + “,” + $TenantOURootPath)
$Memberof=“CN=” + $CompanyName + " " + $ADGroupType1 + “,” +$MSPMailTargetOU

If I check any of the variables above by just typing the variable name, the values look as I would want them, however when I run the following command:

[PS] C:\Tools\CopyAD>New-GlobalAddressList ($CompanyName + " Global Address List") -RecipientFilter {(memberofgroup -eq

My new global address list is created but with a recipientfilter value of “-not(MemberOfGroup -ne $null)”. I’m confused (not to say annoyed!). Is what I’m trying to do, given the amount of variables I’m trying to join up, even possible?

Thanks in advance for any advice you may have.

I don’t usually concatenate at all. Use double quotes and subexpressions.

$final = “This is a $piece of $($”

Variables can go right into double quotes. No need to concatenate.

I’ve got a very similar problem. I work for a service provider that creates new customers regularly, so I’m trying to write a script to help automate this. I’m building an email address policy based on certain variables which are entered by the administrator using read-host. The variables are being set ok, so what I end up with is this:

$AddressFormat=%g (or whatever the desired format is)
$ (again, whatever it is for any given customer)

Now using the above variables, this works:
New-EmailAddressPolicy -name ($OUName + ’ Policy’) -EnabledEmailAddressTemplates (‘SMTP:’ + $AddressFormat + ‘@’ + $EmailDomain) -RecipientFilter (“MemberOfGroup -eq ‘$GroupPath’”)

But later on when I’m building global address lists etc, I need to add in other bits, such as:

(“MemberOfGroup -eq ‘$GroupPath’” -and ((Alias -ne $null) -and ((RecipientDisplayType -eq ‘ConferenceRoomMailbox’) -or (RecipientDisplayType -eq ‘SyncedConferenceRoomMailbox’)))

For the life of me I cannot make this or any other variation that I’m aware of work so that I can build criteria for the object I’m creating. How would I do this using variables and multiple factors with the -RecipientFilter?

Many thanks!

I was in such a rush to get this posted, I didn’t realise that the original post was mine! (not entirely sure why I haven’t answered this previously to say that my script had moved on though). So forgetting the first problem, it’s moved on to the later comment I’ve made on the issue. Thanks.

"(MemberOfGroup -eq '$GroupPath' -and ((Alias -ne `$null) -and ((RecipientDisplayType -eq 'ConferenceRoomMailbox') -or (RecipientDisplayType -eq 'SyncedConferenceRoomMailbox')))"

When it was just like this:

"MemberOfGroup -eq '$GroupPath'"

Say $GroupPath equals CN=MyGroup,OU=MyOU,DC=domain,DC=com, what you are passing the cmdlet is:

"MemberOfGroup -eq 'CN=MyGroup,OU=MyOU,DC=domain,DC=com'"

You are passing it a string with some simple comparisons. The more complex one you posted isn’t surrounded by quotes and if you ran it by itself it would error out. You have to build the whole thing as a string. And if it is a double quoted string, $null will expand out to the actual null value unless you escape the $ before it.