0

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?

2 Answers 2

2

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.

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

4 Comments

"$($_.proxyaddresses)" -match 'sip:' would do the same thing. arrays become space delimited strings when flattened in PowerShell ...for the most part.
@Matt I often prefer using -Join when dealing with the custom objects powershell splits back, sometimes you end up with msProxyAddress[] wheras -join seems to call .ToString() on each item individually (or something)
indeed. that is why i trailed ...for the most part.
Many thanks for this. I've tested and both answers work fine. I opted to use the below answer however as regex is something I'd like to expand on so using it as much as I can in my scripts. Cheers
1

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.

Comments

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.