MUY Belgium
MUY Belgium

Reputation: 2452

OR and AND operator inside ? filter

I have a long program which does as such:

[xml]$data = get-content "c:\somedatafile.xml"
$data.PRINTERS.PRINTER `
| ? { $_.USERS.USER -eq "$env:username" } `
| ForEach-Object{
      # install some printer with $_
}

I have also some COMPUTERS.COMPUTER to be compared with $env:computername.

How can I install the printer if at least one of the COMPUTERS.COMPUTER -eq "$env:computername"? Or how do I include OR and AND, and possibly () inside the ? filter.

Where can the meaning of the ? filter be found out (Google fails with monocarachter queries)?

Suggestions about code sanity are welcome.

Upvotes: 1

Views: 10339

Answers (2)

MUY Belgium
MUY Belgium

Reputation: 2452

Please see Using the Where-Object Cmdlet

Get-Process | Where-Object { ($_.handles -gt 200) -and ($_.name -eq "svchost") }

Note that there is no associative operator is () (as not written on this page).

Upvotes: 1

Patrick
Patrick

Reputation: 2208

Here your snippet more readable:

[xml]$data = get-content "c:\somedatafile.xml"
$data.PRINTERS.PRINTER | Where-Object -FilterScript { $_.USERS.USER -eq "$env:username" } | ForEach-Object {
      # install some printer with $_
}


No you have to add an neq If statement in the foreach and check if COMPUTERS.COMPUTER -eq "$env:computername". If true, install the driver.

Upvotes: 0

Related Questions