10

我试图找出一种方法来让这个命令从一组值而不是一个值中过滤。目前这就是我的代码的样子(当 $ExcludeVerA 是一个值时它可以工作):

$ExcludeVerA = "7"

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

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

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

foreach ($x in $ExcludeVerA)
{

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

}

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

4

2 回答 2

17

尝试-notcontains

where ({ $ExcludeVerA -notcontains $_.Version })

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

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

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

那是对你问题的直接回答。可能的解决方案可能是这样的:

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

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

于 2013-05-07T13:27:55.210 回答
3

尝试这个:

Get-WmiObject -Class Win32_Product -Filter "Name LIKE '%Java%'" | 
Where-Object {$_.Version -notmatch '[734]'}
于 2013-05-07T13:37:25.647 回答