Justin Beagley
Justin Beagley

Reputation: 299

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,

Upvotes: 1

Views: 1087

Answers (1)

Samuel Prout
Samuel Prout

Reputation: 665

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

Upvotes: 1

Related Questions