Powershell help filtering an existing Array

jbla9028
jbla9028 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon IT
Awarded 2018
Distinguished Expert 2018
Commented:
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

Lead SaaS Infrastructure Engineer
Commented:
$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

Author

Commented:
Thanks guys. I Appreciate the help!  I was able to finish the script with your suggestions. Thank you very much.

Author

Commented:
Thanks guys. I Appreciate the help!  I was able to finish the script with your suggestions. Thank you very much.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Glad to help :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial