使用Powershell"where"命令与数组值进行比较

Thr*_*ase 10 arrays powershell foreach where

我试图想办法让这个命令从一个值数组中过滤而不是一个值.目前这是我的代码的方式(当$ ExcludeVerA是一个值时它可以工作):

$ExcludeVerA = "7"

$java = Get-WmiObject -Class win32_product | where { $_.Name -like "*Java*"} |
where ({ $_.Version -notlike "$ExcludeVerA*" })
Run Code Online (Sandbox Code Playgroud)

我希望$ ExcludeVerA有一个像这样的值数组(这当前不起作用):

$ExcludeVerA = "7", "3", "4"

foreach ($x in $ExcludeVerA)
{

$java = Get-WmiObject -Class win32_product | where { $_.Name -like "*Java*"} |
where ({ $_.Version -notlike "$ExcludeVerA*" })

}
Run Code Online (Sandbox Code Playgroud)

关于为什么第二块代码不起作用或者我能做什么的其他想法的任何想法?

ste*_*tej 17

尝试 -notcontains

where ({ $ExcludeVerA -notcontains $_.Version })
Run Code Online (Sandbox Code Playgroud)

所以,如果我理解它,那么

$ExcludeVerA = "7", "3", "4"

$java = Get-WmiObject -Class win32_product | where { $_.Name -like "*Java*"} |
where ({ $ExcludeVerA -notcontains $_.Version })
Run Code Online (Sandbox Code Playgroud)

这是你问题的直接答案.可能的解决方案可能是这样的:

$ExcludeVerA = "^(7|3|4)\."
$java = Get-WmiObject -Class win32_product | 
          where { $_.Name -like "*Java*"} |
          where { $_.Version -notmatch $ExcludeVerA}
Run Code Online (Sandbox Code Playgroud)

它使用正则表达式来完成工作.