Filtering Enabled only Get-ADObject to with specific patterns and OU?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by ITEngineer »

Hi All,

I need to get the Enabled AD Objects (Users & Computer) that does not include the specific name patterns.

How can I filter out or exclude some of the results using the Get-ADObject with the below Query?

Script:

Code: Select all

$Exclusions = @(
   'SystemMailbox',
   'HealthMailbox',
   'Migration'
   'Delete'
   'Disabled'
)
Get-ADObject -Filter '(ObjectClass -eq "user" -or ObjectClass -eq "computer") -and Enabled -eq $true -and isRecycled -eq $false -and name -ne "Deleted Objects"' | Where-Object{$_.Name -notin $Exclusions}
Issues with the script above:
1. When I add the Filter Enabled -eq $true, nothing is returned.
2. I wanted to exclude certain OU like ‘OU=Disabled Users‘
3. If the name contains anything like the above $Exclusions

Thank you in advance.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by jvierra »

You are missing commas in your exclusion list.

The following will be better.

Code: Select all

$Exclusions = @(
   'SystemMailbox',
   'HealthMailbox',
   'Migration',
   'Delete',
   'Disabled',
   'Deleted Objects'
)
$filter = '
    (
        ObjectClass -eq "user" -or
        ObjectClass -eq "computer"
    ) -and 
    Enabled -eq $true -and 
    isRecycled -eq $false
' 
Get-ADObject -Filter $filter |
    Where-Object{$_.Name -notin $Exclusions}
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by ITEngineer »

Thank you Mr. Vierra,

Somehow it does not return any result?
even after removing the Enabled -eq $true

I've also changed it into Where-Object{$_.Name -notcontains $Exclusions}
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by jvierra »

YOU are guessing ... Your guesses have nothing to do with the issue.

If the filter fails then there are no objects that match the criteria. I suspect you need to remove the "isRecycled" to fix this.

To test a filter add only one item at a time to discover the restrictive item.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by ITEngineer »

Yes, already did.
  1. $filter = '
  2.    (
  3.        ObjectClass -eq "user" -or
  4.        ObjectClass -eq "computer"
  5.    ) -and
  6.    Enabled -eq $true
  7. '
  8. Get-ADObject -Filter $filter
The script above returns all Users and Computers object.

but when I add Enabled -eq $true, it does not returns anything at all?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by jvierra »

All objects do not support the "Enabled" attribute. If the attribute is missing then it will not be true.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by ITEngineer »

jvierra wrote: Wed Aug 19, 2020 7:32 pm All objects do not support the "Enabled" attribute. If the attribute is missing then it will not be true.
That makes sense.

Thank you Mr. Vierra :-)
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by jvierra »

I think the issue is that the Get-AdUser and Get-AdComputer commands add the "Enabled" property by converting the flags but Get-AdObject does not do this. You will have to extract the attribute and test the bit after extracting the objects.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by ITEngineer »

jvierra wrote: Wed Aug 19, 2020 8:04 pm I think the issue is that the Get-AdUser and Get-AdComputer commands add the "Enabled" property by converting the flags but Get-AdObject does not do this. You will have to extract the attribute and test the bit after extracting the objects.
Yes, that's too complex for me to do :-o
hence I was just asking it if it were possible with one-liner command. ;)
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Filtering Enabled only Get-ADObject to with specific patterns and OU?

Post by jvierra »

You asked this same question in another forum. The answer given gave you the steps to diagnosing why you were not getting any results. Have you done what was requested. Do you get result when retrieving without the "Where" filter?
This topic is 3 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked