Powershell参数的变量扩展 [英] Powershell variable expansion in parameters

查看:53
本文介绍了Powershell参数的变量扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此powershell脚本来处理交换邮箱,并且其中一个参数(在我必须运行的许多命令中)需要在RecipientFilter下嵌入一个变量.无论我执行哪种类型的扩展,它都始终按字面值对其进行评估(如$ prefix),而不是对其进行扩展.我不确定是否有一种特殊的方法可以逃脱它,或者是什么.想法?

  $ prefix ="FAKEDOM"New-AddressList -name"AL _ $ {prefix} _Contacts" -RecipientFilter {(RecipientType -eq'MailContact')-and(CustomAttribute15 -eq $ prefix)} 

固定变量名.请注意,问题是第二次使用$ prefix,第一个正在正常使用.

有效的解决方案:

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

解决方案

您的变量(至少在示例代码中)被命名为 $ prefix ,而不是 $ fakedom .您需要在扩展中使用正确的变量名.

还要注意,下划线字符将被视为替换字符串中变量名称的一部分,因此您将需要使用 $($ variableName) $ {variableName} .也就是说,您不能像这样进行替换:"vanilla_ $ variableName_strawberry" ,Powershell将查找名为 variableName_Strawberry"的变量,该变量不存在./p>

全部,这就是您所需要的:

  $ prefix ="FAKEDOM"New-AddressList -name"AL _ $ {prefix} _Contacts" -RecipientFilter {(RecipientType -eq'MailContact')-and(CustomAttribute15 -eq $ prefix)} 

修改

您的编辑清楚地表明,第一次使用 $ prefix 很好,并且是引起问题的筛选器. -RecipientFilter 是一个 string 属性,但是您并未将过滤器表达式括在任何形式的引号中.而是使用 {} 括号.通常,这很好,但是当通过 {} 中括号指定字符串时,Powershell不会扩展变量.

  PS>函数Parrot([string] $ Message){$ message}PS>$ food ='饼干'PS>鹦鹉{多莉想要一份$ food吗?}波莉想要一份食物吗? 

您需要更改为使用双引号:

  PS>鹦鹉波莉想要食物吗?"波莉想要饼干吗? 

因此您的过滤器应如下所示(在 $ prefix 值周围添加内部单引号):

  -RecipientFilter((RecipientType -eq'MailContact')-and(CustomAttribute15 -eq'$ prefix')" 

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}`" )"

解决方案

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')"

这篇关于Powershell参数的变量扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆