Powershell 对象的位置-或不工作

Powershell 对象的位置-或不工作,powershell,Powershell,好吧,我工作的公司已经收购了很多域名。每个域都有不同的命名模式。(例如:john.smith,jsmith)-当我们终止某人时,我想自动禁用他们在每个域中的帐户 在我在where object cmdlet中添加'or'语句之前,该脚本工作得非常好 $user1 = "John.smith" $user2 = "Jsmith" get-aduser -Filter * | Where-Object {{$_.samaccountname -eq "$User1"} -or {$_.samacco

好吧,我工作的公司已经收购了很多域名。每个域都有不同的命名模式。(例如:john.smith,jsmith)-当我们终止某人时,我想自动禁用他们在每个域中的帐户

在我在where object cmdlet中添加'or'语句之前,该脚本工作得非常好

$user1 = "John.smith"
$user2 = "Jsmith"
get-aduser -Filter * | Where-Object {{$_.samaccountname -eq "$User1"} -or {$_.samaccountname -eq "$user2"}}
如果我取出or语句,它就会找到用户。如果我放入or语句,它只会返回域中的每个用户

我还尝试避免使用-or,并使用if,else语句:

$user1 = "john.smith"
$user2 = "jsmith" 
$result = Get-ADUser -filter * | Where-Object {$_.samaccountname -eq "$user1"} 
if($result = $error) {Get-ADUser -Filter * | Where-Object {$_.samaccountname -eq "$user2"}}
这不会返回错误-它只是不返回任何内容(可能是if语句出错-但为什么没有错误


谢谢,

您的Where对象中有一组额外的括号。您要使用的是括号,但没有括号也可以:

get-aduser -Filter * | Where-Object {$_.samaccountname -eq "$User1" -or $_.samaccountname -eq "$user2"}

好的-这很有效。奇怪,因为我以前看到过在where对象中添加多个参数的双括号。我想这是一个糟糕的形式。非常感谢。