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}`" )"
localhost
  • 357
  • 2
  • 8

3 Answers3

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')"
latkin
  • 16,402
  • 1
  • 47
  • 62
  • 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. – localhost Oct 26 '12 at 17:53
  • That's what i initially thought but it looks like it's not actually parsing it as a string. If i update it to the format you specified, it errors saying that it's looking for a System.Object. – localhost Oct 26 '12 at 18:20
  • According to http://technet.microsoft.com/en-us/library/aa996912.aspx the parameter is a string. Does it work if you hard code in `FAKEDOM` instead of using a variable? – latkin Oct 26 '12 at 18:25
  • I must be off my game today, it was partially a typo, and also needed to be adjusted just a little more. I posted the final version at the top that is now working, thanks for your help. – localhost Oct 26 '12 at 18:40
  • @Devtron: words win in today's world, unfortunately. – Victor Zakharov Oct 26 '12 at 18:59
  • 1
    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. – latkin Oct 26 '12 at 19:15
  • @Neolisk both are valid answers. But this answer explains why it is a valid answer. It's like doing someone else's homework. You can do it for them, or you can show them how to do it themselves. – D3vtr0n Oct 26 '12 at 19:34
  • @Devtron: You cannot argue that time matters. I answered 6 minutes after the question. And there is nothing to explain for this problem, IMHO. – Victor Zakharov Oct 26 '12 at 19:36
  • 1
    @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). – latkin Oct 26 '12 at 20:05
  • 1
    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. – D3vtr0n Oct 26 '12 at 20:49
  • 1
    @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. – Victor Zakharov Oct 26 '12 at 20:53
2

Try this:

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

Notice the extra dollar sign I added.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • There is no variable named `fakedom`, at least not in the code OP posted. – latkin Oct 26 '12 at 17:31
  • @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! – Victor Zakharov Oct 26 '12 at 17:43
  • 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. – localhost Oct 26 '12 at 17:50
  • 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 Oct 26 '12 at 17:50
  • @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. – Victor Zakharov Oct 26 '12 at 17:55
  • @Neolisk - I agree that's a useful skill, you should work on acquiring it (hours later, your answer still does not address the OP's issue). – latkin Oct 26 '12 at 20:15
  • @latkin: I will work on it. And you try to be polite next time, okay? – Victor Zakharov Oct 26 '12 at 20:44
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)}
WhiteKnight
  • 4,938
  • 5
  • 37
  • 41