I was scrolling X (aka Twitter) today and saw this blog post, PowerShell: Report On-Premises Active Directory Accounts that are Synchronized with Azure AD Connect, by Kevin Trent. I like reading blog posts as I tend to learn some new things and see how people tend to solve their problems. Upon reading the provided code, two things stood out to me:
- usage of the AzureAD module, which is going to stop working on March 2024 (if Microsoft won't change it again)
- using Get-AzureADUser inside the Select-Object statement
Here's what that solution looks like:
Import-Module ActiveDirectory
Connect-AzureAD
Get-ADUser -Filter {Enabled -EQ $True} -Properties * |
Select-Object DisplayName, SamAccountName, UserPrincipalName, LastLogonDate,
@{N="AzureADSynced"; E={(Get-AzureADUser -ObjectID $_.UserPrincipalName |
Select-Object -Property DirSyncEnabled).DirsyncEnabled}} |
Export-Csv $env:userprofile\documents\On-Prem_CloudSynced_Accounts.csv
While this solution will work for the next couple of months and may work for 200 users, Kevin mentioned it would have difficulty querying 1000, 10000, or 50000 users. It will either take hours to finish or never finish at all. Aside from the obvious that for each user, a call will need to be made to Azure AD to get just one property, Active Directory doesn't like a pipeline. It may work very well most of the time, but as soon as something runs longer, it will start throwing errors.
It will not happen every time, maybe even never, but if it will, you will spend hours debugging what's wrong and how to fix it. When working with the ActiveDirectory module, I wasted lots of time finally dropping the pipeline altogether.