0

I'm currently using Powershell, and i'm trying to sort data in my variable that i'm pulling from another system. At the moment i'm trying to use one -and statement and -or statements after an -eq sign. So I was wondering what's the correct way I need to make the syntax in my where statement.

$DLPList | Select Node.NodeName, Properties.OSType, PropsView.version, Node.NodeTextPath2 | where PropsView.version -ne '1.4.706.172' -and (Properties.OSType -eq $win7 -or $win8 -or $win81 -or $win10)

I know there's multiple other stuff on this, but everything i've tried so far doesn't work. Thanks for the help.

2
  • 4
    does your Select-Object section work? i have never seen Property.SubProp used before. for the -or stuff ... you CANNOT combine them like that. you could likely use $Thing -in @($OneThing, $TwoThing, $ThreeThing). Commented Jun 14, 2019 at 21:20
  • FWIW, it seems @trevor-lines answered his question here: stackoverflow.com/a/56634283/182742 Commented Jun 17, 2019 at 22:08

3 Answers 3

1

Yes, you have to use the script block version of where-object. Also note that -and and -or have EQUAL PRECEDENCE in powershell, which is very unusual in a language.

Sign up to request clarification or add additional context in comments.

Comments

0

Alright, what I figured out for this is that you cannot just add use -or. Instead you must do Properties.ostype for each one. Here is the code that I came up with to get it to work. Also a little tip is to do Where-Object before Select-Object to save computing power.

$DLPList = $list1 | Where-Object{ $_.'UDLP.prodversion' -lt '9.5.704.112' -and ($_.'Properties.OSType' -ne 'Windows XP') -and ($_.'EPOComputerProperties.OSType' -ne 'Windows Vista') -and ($_.'EPOComputerProperties.OSType' -ne 'Windows 2003') -and ($_.'Properties.OSType' -ne 'Windows 2003 R2') -and ($_.'EPOComputerProperties.OSType' -ne 'Windows 2008') -and ($_.'Properties.OSType' -ne 'Windows 2008 R2') -and ($_.'Properties.OSType' -ne 'Windows Server 2012' -and ($_.'Properties.OSType' -ne 'Windows Server 2012 R2') -and ($_.'Properties.OSType' -ne 'Windows Server 2016')) } | Select-Object Node.NodeName, Properties.OSType, UDLP.productversion, Node.NodeText | Sort-Object -Property Node.NodeText -Descending

Comments

-1

Try adding brackets and replacing the chained boolean operators with a regular expression "-match" operator condition:

$DLPList | Select Node.NodeName, Properties.OSType, PropsView.version, Node.NodeTextPath2 | where { $_.'PropsView.version' -ne '1.4.706.172' -and ($_.'Properties.OSType' -match "($win7|$win8|$win81|$win10)") }

Here's sample code that works for me:

$DLPList = @(2, 4, 6, 7)
$DLPList | where { ($_ -ne 7) -and ($_ -lt 10) -and ($_ -gt 0) }
2
4
6

$DLPList | where { ($_ -ne 7) -and ($_ -match "(4|5|11)" -and ($_ -gt 0)) }
4

4 Comments

that 2nd line is wrong. [sigh ...] it will give you 2,4,6 ... and both 2 & 6 aint correct results. the reason is that the 2nd part does $_ -eq 4 and then compares the boolean from that with -or 5. ///// you are NOT doing what you seem to think you are doing. [grin]
@Lee_Dailey Of course, you are right; thank you. I've updated the answer to use the regular expression "-match" operator.
kool! glad to see it fixed & working ... [grin] that is a classic error with chaining logical operators - i've done it more than once!
Wouldn't that be $_.PropsView.version and $_.Properties.OSType?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.