Powershell在$ _ -like $ Var中扩展变量

时间:2012-12-24 14:52:56

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v1.0

好的我正尝试做以下事情:

简化示例:

$Var = "MyProduct*MyProduct"
$List += "MyProduct 11 MyProduct"
$List += "YourProduct 11 YourProduct"
$List += "SomethingElse"
$NewVar = $List | Where {$_ -like "$Var"}

我希望扩展$ Var中的“*”,然后检查它是否像“$ _”那样它会识别var中有一个通配符并根据它得到结果..有什么办法吗?这样做?

1 个答案:

答案 0 :(得分:4)

$ list不是一个数组,每次添加它都会实际连接到它。您可以使用一元逗号运算符从单个项目数组开始(这只是一种方法),现在每个添加都将添加一个新的数组项:

PS> $List = ,"MyProduct 11 MyProduct"
PS> $List += "YourProduct 11 YourProduct"
PS> $List += "SomethingElse"
PS> $list
MyProduct 11 MyProduct
YourProduct 11 YourProduct
SomethingElse

PS> $List -like $var
MyProduct 11 MyProduct