Powershell help filtering an existing Array

I have an existing Array that I've created with the following code (helped by another member of EE:)

Foreach ($ou in $OUList){$results = Get-ADUser -SearchBase $ou -Filter { memberof -ne $HitachiGroupDN -and memberof -ne $TestAccountsGroupDN -and memberof -ne $ServiceAccountsGroupDN -and enabled -eq 'True'} -Properties  mail,GivenName,sn,Department,WhenCreated,thumbnailphoto}

Open in new window


I have another script that I need to take this same list, and figure out who doesn't have a value in the AD Attribute "thumbnailphoto". How do I pass the variable back in ? would I use a foreach and go back through the $results variable?

Any help would be appreciated.
LVL 1
jbla9028Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
I'd do something like this:

#Saving all the people in the OU with the previous query into a hash table
$AllPeopleInOU=@()
Foreach ($ou in $OUList){$AllPeopleInOU+= Get-ADUser -SearchBase $ou -Filter { memberof -ne $HitachiGroupDN -and memberof -ne $TestAccountsGroupDN -and memberof -ne $ServiceAccountsGroupDN -and enabled -eq 'True'} -Properties  mail,GivenName,sn,Department,WhenCreated,thumbnailphoto}

#then save all that have the null value 
$PeopleWithoutThumbNailPhoto=@()
foreach($person in $AllPeopleInOU){
   if($person.thumbnailphoto){
      $PeopleWithoutThumbNailPhoto+=$person
   }
}
#showing results
write-host "all the people without ThumNailPhoto"
$PeopleWithoutThumbNailPhoto

Open in new window

0
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
$PeopleWithout Thumbails

I'd actually have done it freshly because it would take less time if you add the no-thumbnails to the filter.

Because of the way the other expert helped you set up this query, you are over-writing the variable so you only capture the results of the last OU you queried.

This would get all the results for all the OUs in your list, and only for the users who do not have a thumbnail:


$NoThumbnails=@()
Foreach ($ou in $OUList){
	$NoThumbnails += Get-ADUser -SearchBase $ou -Properties  mail,GivenName,sn,Department,WhenCreated,thumbnailphoto -Filter {
		thumbnailphoto -notlike "*" -and memberof -ne $HitachiGroupDN -and memberof -ne $TestAccountsGroupDN -and memberof -ne $ServiceAccountsGroupDN -and enabled -eq 'True'
	} 
}

Open in new window

If you need all that list of users for some other reason, and the no-thumbs is a separate step, then you should still amend your original code so you get all results form all OUs.

  Then you and quickly separate the Users without a Thumbnail to an additional variable as shown with a simple filter of the variable's content using a where clause as shown:

$NoThumbnails=@()
$Results=@()

Foreach ($ou in $OUList){
	$Results += Get-ADUser -SearchBase $ou -Properties  mail,GivenName,sn,Department,WhenCreated,thumbnailphoto -Filter {
		memberof -ne $HitachiGroupDN -and memberof -ne $TestAccountsGroupDN -and memberof -ne $ServiceAccountsGroupDN -and enabled -eq 'True'
	} 
}

$NoThumbnails=$Results | ? {$_.thumbnailphoto -eq $Null}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jbla9028Author Commented:
Thanks guys. I Appreciate the help!  I was able to finish the script with your suggestions. Thank you very much.
1
jbla9028Author Commented:
Thanks guys. I Appreciate the help!  I was able to finish the script with your suggestions. Thank you very much.
0
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Glad to help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.