3

I'm working on this powershell script to deal with exchange mailboxes, and one of the parameters (in a lot of the commands i have to run) needs a variable embedded in it under RecipientFilter. No matter what type of expansion i do, it always evaluates it literally (as $prefix) instead of expanding it. I'm not sure if there's a special way i'm supposed to escape it, or what. Ideas?

$prefix="FAKEDOM"

New-AddressList -name "AL_${prefix}_Contacts" -RecipientFilter {(RecipientType -eq 'MailContact') -and (CustomAttribute15 -eq $prefix)}

Edit: fixed variable name. Note that the question is for the second use of $prefix, the first one is working correctly.

Edit: Working solution:

 New-AddressList -name "AL_${prefix}_Contacts" -RecipientFilter "(RecipientType -eq 'MailContact') -and (CustomAttribute15 -eq `"${prefix}`" )"
2
  • Your code is updated, but the question text still mentions $fakedom. Commented Oct 26, 2012 at 18:04
  • sigh, updated once more, thanks Commented Oct 26, 2012 at 18:09

3 Answers 3

5

Your variable (at least in the sample code) is named $prefix, not $fakedom. You need to use the proper variable name in your expansion.

Also note that the underscore char will be assumed to be part of the variable name in the replacement string, so you will need to use either $($variableName) or ${variableName}. That is, you can't do a replacement like this: "vanilla_$variableName_strawberry", Powershell will look for a variable named variableName_Strawberry", which doesn't exist.

All up, this is what you need:

$prefix="FAKEDOM"

New-AddressList -name "AL_${prefix}_Contacts" -RecipientFilter {(RecipientType -eq 'MailContact') -and (CustomAttribute15 -eq $prefix)}

Edit

Your edit makes it clear the first use of $prefix is fine, and it's the filter that is causing trouble. -RecipientFilter is a string property, but you are not enclosing your filter expression in any kind of quotes. Instead you use {} brackets. That's fine in general, but Powershell will not expand variables when your string is specified via {} brackets.

PS> function Parrot([string] $Message){ $message }
PS> $food = 'cracker'
PS> Parrot {Polly want a $food ?}
Polly want a $food ?

You need to change to using double-quotes:

PS> Parrot "Polly want a $food ?"
Polly want a cracker ?

So your filter should look like below (adding inner single quotes around the $prefix value):

-RecipientFilter "(RecipientType -eq 'MailContact') -and (CustomAttribute15 -eq '$prefix')"
Sign up to request clarification or add additional context in comments.

11 Comments

I edited the original post, the variable and braces were my mistake. The overall issue is the second use of $prefix (at the end of the code), the first ${prefix} is working correctly.
Glad this worked, I see it was additionally a matter of quoting within the query. I will update to show that. @Neolisk - Answers which solve askers' problems, give example code, and link to relevant official documentation win in today's world.
@Neolisk - Do you mean you cannot argue that time doesn't matter? Regardless, your answer made unstated (ultimately incorrect) assumptions, and is still is not updated to address what the OP has now made clear is the real issue (the filter, not the -Name param).
the reason I commented in the first place was because @latkin provided some nice detail on his answer. I respect that a lot more than no explanation at all. For someone trying to learn Powershell, this type of answer is best.
@Devtron: Well, the reason I commented was the attitude of Mr. latkin. Being aggressive and trying to educate everybody is not how you solve problems. First a downvote, then a bloated explanation of how cool he is. Even if I did make a mistake in my assumption, it is not a good reason to behave like that. Further discussion is not part of SO, so let's finish it up right here, right now.
|
2

Try this:

New-AddressList -name "AL_$($prefix)_Contacts" ...

Notice the extra dollar sign I added.

7 Comments

There is no variable named fakedom, at least not in the code OP posted.
@latkin: Then the OP did not post all the code he had. Is it a big deal? I am just showing the concept. And thanks for downvoting a correct solution to whomever did that. You guys are amazing!
My mistake, i had a few versions of this working and pasted the wrong variable. The main post is updated, it's the second part (at the end) that's not working. The $(prefix) is working fine in the -name parameter.
The OP has included all code it seems - the variable is simply named prefix, not fakedom. If you think there is some missing code, then I would recommend calling that out in your answer.
@latkin: there is a very useful skill to see the core of the problem, which comes with age and experience. Especially if you dealt with business world, you know not to pick at missing semicolons etc. I updated my answer with proper variable name.
|
0

Neither $($variableName) nor ${variableName} worked for me for variable expansion in parameters with Powershell 4 but the following did:

$prefix="FAKEDOM"

New-AddressList -name ("AL_" + $prefix + "_Contacts") -RecipientFilter {(RecipientType -eq 'MailContact') -and (CustomAttribute15 -eq $prefix)}

Comments

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.