gpt4 book ai didi

powershell - where子句中的多个条件

转载 作者:行者123 更新时间:2023-12-03 00:25:05 25 4
gpt4 key购买 nike

当我执行以下代码时,出现以下错误:找不到与名称或匹配的参数。如何获得电子邮件地址中没有abc.com或xyz.com的用户列表?

Get-ADGroupMember -Identity "AQ" -Recursive | where objectClass -eq 'user' | Get-ADUser -Properties *,  "msDS-UserPasswordExpiryTimeComputed", PasswordNeverExpires |

where mail -notmatch "@abc.com" -or "@xyz.com" |
Select-Object @{Label = "SAM Account Name";Expression = {$_.SamAccountName}}

最佳答案

花括号(实际上是带有过滤器脚本的脚本块)不能总是通过Where-Object来跳过。

您可以执行Where-Object objectClass -eq 'user',但涉及多个操作符的所有事情都需要编写为过滤器脚本:

where {$_.mail -notmatch "@abc.com" -or "@xyz.com" }

现在,此逻辑不起作用,因为它等效于以下语句:
where {($_.mail -notmatch "@abc.com") -or $true }

因此,无论 -notmatch操作的结果如何,您的where子句均为true。您需要两个 -notmatch操作:
Where-Object - { $_.Mail -notmatch '@abc.com' -and $_.Mail -notmatch '@xyz.com' }

根据要在过滤器脚本中排除的电子邮件地址数量,您可能需要使用其他方法:从电子邮件地址中删除用户名,然后查看该地址是否出现在您所指定的电子邮件地址数组中想要排除。
Where-Object { ( $_.Mail -replace '^[^@]+') -notin '@abc.com','@xyz.com','@foo.bar' }

关于powershell - where子句中的多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53610075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com