# Getting Where-Object to filter correctly

**URL:** <https://forums.powershell.org/t/getting-where-object-to-filter-correctly/11796>\
**Category:** PowerShell Help\
**Created:** [December 16, 2018, 4:03am UTC](https://forums.powershell.org/t/getting-where-object-to-filter-correctly/11796 "2018-12-16T04:03:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![qprjack86](https://avatars.discourse-cdn.com/v4/letter/q/9d8465/32.png) [@qprjack86](https://forums.powershell.org/u/qprjack86)\
**Post date:** [December 16, 2018, 4:03am UTC](https://forums.powershell.org/t/getting-where-object-to-filter-correctly/11796/1 "2018-12-16T04:03:20Z")

</div>

Hi

I have a script that checks for iLO firmware versions and updates if they are not running the latest versions.

I’m getting some strange behavior when using Where-Object to check for firmware versions.

For example, the below works fine, and on a server with the firmware versions listed nothing is returned:

```
$global:iLOType=(Find-HPEiLO-Range$global:iLOs-WarningActionSilentlyContinue).PN$global:iLO3fwVersion="1.91"$global:iLO4fwVersion="2.61"$foundServers=Find-HPEiLO-Range$global:iLOs|Where-Object{($\_.PN-EQ$global:iLOType)-and($\_.FWRI-NE$global:iLO4fwVersion)-or($\_.PN-EQ$global:iLOType)-and($\_.FWRI-NE$global:iLO3fwVersion)}
```

However, I wanted to add a check for iLO 5 machines so added:

```
$foundServers=Find-HPEiLO-Range$global:iLOs|Where-Object{($\_.PN-EQ$global:iLOType)-and($\_.FWRI-NE$global:iLO4fwVersion)-or($\_.PN-EQ$global:iLOType)-and($\_.FWRI-NE$global:iLO3fwVersion)-or($\_.PN-EQ$global:iLOType)-and($\_.FWRI-NE$global:iLO5fwVersion)}
```
But now when the script is run the script outputs the following to the $foundservers variable:
```
FWRI:1.91PN:IntegratedLights-Out3(iLO3)
```
Have tried a couple of different options to get this to work such as:

```
Find-HPEiLO-Range$global:iLOs|Where-Object{($\_.PN-EQ$global:iLOType)-and($\_.FWRI-ne($global:iLO3fwVersion)-or($global:iLO4fwVersion)-or($global:iLO5fwVersion))}
```
and:
```
Find-HPEiLO-Range$global:iLOs|Where-Object{($\_.PN-EQ$global:iLOType)-and(($\_.FWRI-ne$global:iLO3fwVersion)-or($\_.FWRI-ne$global:iLO4fwVersion)-or($\_.FWRI-ne$global:iLO5fwVersion))}
```
But all seem to output the FWRI and PN values regardless.

Here’s a link to the full script for completeness:

[https://1drv.ms/u/s!AvSxePbq1EpYq51IHeTBX8A9zojk7g](https://1drv.ms/u/s!AvSxePbq1EpYq51IHeTBX8A9zojk7g)

---

<div class="post-metadata">

**Author:** ![mortensummer](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mortensummer](https://forums.powershell.org/u/mortensummer)\
**Post date:** [December 16, 2018, 6:49am UTC](https://forums.powershell.org/t/getting-where-object-to-filter-correctly/11796/2 "2018-12-16T06:49:46Z")

</div>

how does this do ?

```
$foundServers = Find-HPEiLO -Range $global:iLOs | Where-Object {
    ($_.PN -EQ $global:iLOType) -and ($global:iLO3fwVersion, $global:iLO4fwVersion, $global:iLO5fwVersion -notcontains $_.FWRI)
```

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [December 16, 2018, 10:50am UTC](https://forums.powershell.org/t/getting-where-object-to-filter-correctly/11796/3 "2018-12-16T10:50:11Z")

</div>

I’m not really sure what all the variables are, but -and and -or will be evaluated left to right unless you add some parentheses. You might think -and would have a stronger precedence than -or, like in other languages. I think you accidentally got the result you wanted in the first case because of this.

```
$true -and $true -or $true -and $false                             
False

$true -and $true -or $true -and $false -or $true -and $true        
True

($true -and $true) -or ($true -and $false)                         
True

($true -and $true) -or ($true -and $false) -or ($true -and $true)  
True
```

---

<div class="post-metadata">

**Author:** ![qprjack86](https://avatars.discourse-cdn.com/v4/letter/q/9d8465/32.png) [@qprjack86](https://forums.powershell.org/u/qprjack86)\
**Post date:** [December 17, 2018, 3:51am UTC](https://forums.powershell.org/t/getting-where-object-to-filter-correctly/11796/4 "2018-12-17T03:51:35Z")

</div>

Thanks for the replies.

Tom\_Mort\_Yates - Many thanks that worked like a charm!

And js thanks for the clarification on how the -if -or works.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/getting-where-object-to-filter-correctly/11796/5 "2024-05-16T20:33:33Z")

</div>

Closed on May 16, 2024, 8:33pm UTC


