ADUser, ADComputer and ADDomainController query optimization

VishnuVardhan SB 1 Reputation point
2022-01-07T18:45:45.753+00:00

Hi All,

I am not a PS guru. I am a beginner in Powershell, so the question may be very basic. I am running the below powershell one-line script on our servers through a cron to fetch the list of ADUser objects. On the same server, I am also running seperate scripts to fetch ADComputer and ADDomainController objects too but on a different cron schedule. The scripts works on one set of servers but on few servers it gets timed out with "Invalid enumeration context" error. I read through some forums and I believe this error is because the numbers of objects retrieved is huge which makes the query to timeout. I am aware that this timeout value can be increased but do not want to do so.

Is there a way I can optimize this query using variables, so that it runs quickly.

script = Get-ADUser -Filter * -ResultSetSize $null -Properties AccountExpirationDate, accountExpires, badPasswordTime, Created, createTimeStamp, DisplayName, DistinguishedName, EmailAddress, Enabled, isDeleted, LastBadPasswordAttempt, lastLogon, LastLogonDate, lastLogonTimestamp, Modified, modifyTimeStamp, Name, PasswordExpired, PasswordLastSet, pwdLastSet, SamAccountName, SID, userAccountControl, UserPrincipalName, whenChanged, whenCreated | Select AccountExpirationDate, accountExpires, badPasswordTime, Created, createTimeStamp, DisplayName, DistinguishedName, EmailAddress, Enabled, isDeleted, LastBadPasswordAttempt, lastLogon, LastLogonDate, lastLogonTimestamp, Modified, modifyTimeStamp, Name, PasswordExpired, PasswordLastSet, pwdLastSet, SamAccountName, SID, userAccountControl, UserPrincipalName, whenChanged, whenCreated

Regards,
Vishnu.

Windows for business | Windows Server | User experience | PowerShell
{count} votes

3 answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-01-07T19:44:41.64+00:00

    Hi @VishnuVardhan SB ,

    how many user objects are in your AD?
    One option could be to split the query using different SearchScopes

    Get-ADUser -Filter * -SearchBase "OU=<UserOU1>,DC=DEMO,DC=LOCAL" -Properties *  
    Get-ADUser -Filter * -SearchBase "OU=<UserOU2>,DC=DEMO,DC=LOCAL" -Properties *  
    Get-ADUser -Filter * -SearchBase "OU=<UserOU3>,DC=DEMO,DC=LOCAL" -Properties *  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 47,901 Reputation points
    2022-01-07T22:10:51.363+00:00

    It may be that the problem is brought about by the needless use of the Select-Object. If you simply use this:

    $Result = Get-ADUser -Filter * -ResultSetSize $null -Properties AccountExpirationDate, accountExpires, badPasswordTime, Created, createTimeStamp, DisplayName, DistinguishedName, EmailAddress, Enabled, isDeleted, LastBadPasswordAttempt, lastLogon, LastLogonDate, lastLogonTimestamp, Modified, modifyTimeStamp, Name, PasswordExpired, PasswordLastSet, pwdLastSet, SamAccountName, SID, userAccountControl, UserPrincipalName, whenChanged, whenCreated
    

    . . . does it work?

    Also, do you have many disabled users? You might want to use a different filter string to get only active users, and if you need disabled users too, run a second Get-ADUser and filter for disabled users.

    Also see this for a good explanation of what may be happening: 32418.active-directory-troubleshooting-server-has-returned-the-following-error-invalid-enumeration-context.aspx


  3. Limitless Technology 39,921 Reputation points
    2022-01-11T08:45:27.497+00:00

    Hi there,

    There is a detailed blog for this issue and approaching a solution indicated in this blog might sort this out. There are two possible solutions to this issue. One of the recommended solutions is to Retrieve your Active Directory objects to a variable first, then send it down the pipeline using the variable. This method is easy to implement in your code without lots of configuration changes in your Active Directory environment.

    https://social.technet.microsoft.com/wiki/contents/articles/32418.active-directory-troubleshooting-server-has-returned-the-following-error-invalid-enumeration-context.aspx

    Here is a thread as well which discusses the same issue and you can try out some troubleshooting steps from this and see if that helps you to sort the Issue.
    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/e3ae9c0d-4eed-4703-b120-14727e797df9/invalid-enumeration-context-using-powershell-script-to-check-computer-accounts?forum=ITCG


    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.