PowerShell:具有多值属性的对象条件



我正在使用以下来针对所有指定条件的相关用户。

$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

我很确定这是返回所有相关信息,但是,这似乎返回了在proxyAddresses属性中确实具有SIP值的用户。

我猜这是因为这是一个多价属性?有什么想法我需要用什么语法来解决这个问题?

您可以使用

$_.ProxyAddresses -contains 'sip:User.One@company.com'

如果您有完整的地址,否则我经常使用的"简单"(但稍微慢)是

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

只需将其作为字符串加入,然后检查中 sip:是否存在。

使用 -match

如果也针对收集作用。它将返回我们可以作为布尔值评估的所有匹配,以满足情况。请注意,这是支撑正则表达式,因此需要逃脱特殊的角色。^是启动字符串的锚点。因此,"^sip"将与" SIP"'。

开始的任何字符串匹配
... | Where-Object {$_.adminDescription -eq "Azure_Sync" -and !($_.proxyAddresses -match "^sip")

因此,括号中的零件如果找到SIP地址,则将其评估为true。您想要相反的

嵌套Where-Object

您可以做的是在proxyaddresses集合中有效的子句中有一个嵌套。在您的用例中,这似乎是更多的努力,但是作为对此答案的一般回应,这将足够。

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

因此,如果内部子句找到了与SIP地址的任何匹配,那么由于!

最新更新