简体   繁体   中英

Where-object -or not working

Alright - so the company i work for has bought out quite a few domains. Each domain has a different naming schema. (ex: john.smith, jsmith) - When we terminate someone, i'd like to automate disabling their accounts in every domain.

This script works perfectly fine until I add the 'or' statement in where-object cmdlet.

$user1 = "John.smith"
$user2 = "Jsmith"
get-aduser -Filter * | Where-Object {{$_.samaccountname -eq "$User1"} -or {$_.samaccountname -eq "$user2"}}

If i take out the or statement - it finds the user. If i put the or statement in, it just returns every user in my domain.

I've also tried avoiding the -or, and went with a if,else statement:

$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"}}

This doesn't return an error - it just doesn't return anything (might be doing the if statement wrong - but why no error?

Thank you,

You have an extra set of brackets inside your Where-Object. What you were going for would work with parenthesis, but it'll work just the same without any:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM