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

问题描述:

我试图找出一种方法来获取此命令从一个值的数组,而不是一个值过滤。目前,这是我的代码是如何(和它的作品时$ ExcludeVerA是一个值):使用Powershell“where”命令与值数组进行比较

$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*" }) 

} 

任何想法,为什么这第二块代码不起作用或我可以做什么的其他想法?

尝试-notcontains

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

所以如果我corretly理解它,然后

$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} 

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

+0

第一种方法行不通,因为$ _这些对象的版本属性通常是像长号:7.01.04756,我需要通过仅第一个数字过滤(即我需要搜索7 *)。 – ThreePhase 2013-05-07 14:57:59

+0

但是,使用正则表达式发布的第二种方式效果非常好!它简单而优雅。它也向我介绍正则表达式,所以谢谢:) – ThreePhase 2013-05-07 14:59:02

试试这个:

Get-WmiObject -Class Win32_Product -Filter "Name LIKE '%Java%'" | 
Where-Object {$_.Version -notmatch '[734]'} 
+0

事情是,我需要它不匹配7 *,而不仅仅是7,因为版本号往往很长(但7之后的东西并不重要。使用正则表达式在Stej的答案中提出了这个窍门,不过谢谢。 – ThreePhase 2013-05-07 15:00:49