Concatenating Variables

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Craig Duff Craig Duff 1 month, 3 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #37210
    Profile photo of Tony Bennett
    Tony Bennett
    Participant

    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"
    $TenantOURootPath="ou=Tenants,DC=exchange14,DC=local"
    $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
    $memberof)}

    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.

    #37325
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

    $final = "This is a $piece of $($object.property)."

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

    #49152
    Profile photo of Tony Bennett
    Tony Bennett
    Participant

    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:

    $OUName=CustomerOUName
    $AddressFormat=%g (or whatever the desired format is)
    $EmailDomain=domain.com (again, whatever it is for any given customer)
    $GroupPath=GroupName

    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!

    • This reply was modified 1 month, 3 weeks ago by Profile photo of Tony Bennett Tony Bennett.
    #49169
    Profile photo of Tony Bennett
    Tony Bennett
    Participant

    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.

    #49171
    Profile photo of Craig Duff
    Craig Duff
    Participant
    "(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.

    • This reply was modified 1 month, 3 weeks ago by Profile photo of Craig Duff Craig Duff.
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.