Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Powershell "where" command to compare against Array of values

I'm trying to figure out a way to get this command to filter from an array of values as opposed to one value. Currently this is how my code is (and it works when $ExcludeVerA is one value):

$ExcludeVerA = "7"

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

And I'd like $ExcludeVerA to have an array of values like so (this currently doesn't work):

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

foreach ($x in $ExcludeVerA)
{

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

}

Any ideas of why this second block of code doesn't work or other ideas of what I can do?

like image 566
ThreePhase Avatar asked May 07 '13 13:05

ThreePhase


2 Answers

Try -notcontains

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

so if I understand it corretly, then

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

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

That was direct answer to your question. Possible solution might be something like this:

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

it uses regex to get job done.

like image 101
stej Avatar answered Oct 15 '22 06:10

stej


Try this:

Get-WmiObject -Class Win32_Product -Filter "Name LIKE '%Java%'" | 
Where-Object {$_.Version -notmatch '[734]'}
like image 40
Shay Levy Avatar answered Oct 15 '22 05:10

Shay Levy