jshizzle
jshizzle

Reputation: 487

PowerShell: Where-Object condition with multi-valued attribute

I'm using the below to target only relevant users with all the specified conditions.

$TargetUsers = Get-ADUser -Filter * -SearchBase $TargetOU -Properties * | Where-Object {$_.adminDescription -eq "Azure_Sync" -and $_.proxyAddresses -notlike "sip*" -and $_.sn -ne $null -and $_."msRTCSIP-PrimaryUserAddress" -ne $null} | Select userPrincipalName, msRTCSIP-PrimaryUserAddress | Sort userPrincipalName

I was pretty sure this was returning all relevant information, however, it seems that this returns users that do have a sip value in the proxyAddresses attribute.

I'm guessing that this is because this is a multi-value attribute? Any ideas what syntax I need to use to address this?

Upvotes: 0

Views: 2488

Answers (2)

Matt
Matt

Reputation: 46730

Use -match

If functions against collections as well. It will return all matches which we can evaluate as a boolean to satisfy the condition. Be warned that is supports regex so special characters will need to be escaped. ^ is an anchor for start of a string. So "^sip" would match any strings that start with "sip".

... | Where-Object {$_.adminDescription -eq "Azure_Sync" -and !($_.proxyAddresses -match "^sip")

So the part in brackets will evaluate to true if it finds a sip address. You want the opposite so we flip the result with a !

Nested Where-Object

What you could do is have a nested where clause that would work on the collection of proxyaddresses. This would seem like more effort in your use case but as a general response to this answer it will suffice.

... | Where-Object{$_.adminDescription -eq "Azure_Sync" -and !($_.proxyaddresses |Where-Object{$_ -like "sip*"}) -and ...

So if the inner clause found any matches with sip addresses then the condition would evaluate to false because of the ! used.

Upvotes: 1

colsw
colsw

Reputation: 3336

You can use

$_.ProxyAddresses -contains 'sip:[email protected]'

if you have the full address, otherwise the "easy" (but slightly slow) solution I often use is

($_.ProxyAddresses -join ' ') -match 'sip:'

which just joins it together as a string and then checks if sip: exists in it.

Upvotes: 2

Related Questions