[英]Powershell Where-Object inserting array
I'm trying to build a Powershell script that can filter the information from a CSV file. 我正在尝试构建一个Powershell脚本,该脚本可以过滤CSV文件中的信息。 But I'm having problems when using an array as one of parameters.
但是在将数组用作参数之一时遇到了问题。
This command works when I import the CSV file and type it in manually: 当我导入CSV文件并手动键入时,此命令有效:
$data |
where-object {
(
$_.PackageAffected -eq "BslEqipWebPkg" -or
$_.PackageAffected -eq "BslIDealAdminPkg" -or
$_.PackageAffected -eq "ALL"
) -and (
$_.ENV -eq "PROD" -or $_.ENV -eq "ALL"
)
} |
select-object -property 'major step id','minor step id','PackageAffected',ENV,detail |
format-table -autosize
I am trying to build the $_.PackageAffected
line up using an array. 我正在尝试使用数组构建
$_.PackageAffected
阵容。
Here is the code so far (that isn't working): 这是到目前为止的代码(不起作用):
$packageList = "`$_.PackageAffected -eq `"" + $package[0] + "`" -or"
for ($element=1; $element -lt $package.count; $element++) {
$packageList = $packageList + " `$_.PackageAffected -eq `"" + $package[$element] + "`" -or"
}
$packageList = $packageList + " `$_.PackageAffected -eq `"ALL`""
$data |
where-object{($packageList) -and ($_.ENV -eq $environment -or $_.ENV -eq "ALL")} |
select-object -property 'major step id','minor step id','PackageAffected',ENV,detail |
format-table -autosize
$packageList
At the bottom where I've put $packageList
it displays onscreen the string I would expect to see, which is: 在我放置
$packageList
的底部,它在屏幕上显示我希望看到的字符串,即:
$_.PackageAffected -eq "pkgName1" -or $_.PackageAffected -eq "pkgName2" -or $_.PackageAffected -eq "ALL"
But when I view the table after it's been run, there are no filters on it for the PackageAffected
. 但是,当我在运行表之后查看该表时,
PackageAffected
上没有任何过滤器。
My suspicions are that I've converted the command into a string and when it runs, Powershell doesn't recognise it as a command. 我怀疑我已将该命令转换为字符串,并且在运行时,Powershell无法将其识别为命令。
You need to evaluate the expression. 您需要评估表达式。 This can be done with Invoke-Expression, eg
这可以通过Invoke-Expression完成,例如
Invoke-Expression $packageList
In the context of your code, this would be: 在您的代码的上下文中,这将是:
$data |
where-object{(Invoke-Expression $packageList) -and ($_.ENV -eq $environment -or $_.ENV -eq "ALL")} |
select-object -property 'major step id','minor step id','PackageAffected',ENV,detail |
format-table -autosize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.