The Daily WTF
Follow
CodeSOD: An Odd Sort
The script aims to report all Active Directory users and their last logon times, but it employs an inefficient and convoluted approach. It iterates through each letter of the alphabet to filter user accounts, which does not guarantee a fully alphabetical sort. For each letter, it creates a new DirectorySearcher object, explicitly requests only the 'name' property, and then finds all matching accounts.Subsequently, for each retrieved name, the script creates yet another DirectorySearcher. This new searcher is used to query for the specific user account to retrieve all its properties. Although only one result is expected for a username search, the FindAll() method is used, returning an array.This requires an unnecessary loop to iterate through a single-element array. Finally, it outputs the user's name and last logon time, concatenated with a comma. The script's design is highly inefficient due to repeated searcher instantiation, redundant queries, and an illogical filtering method. Despite its flaws, its ability to generate a CSV for managers makes it "mission critical" within the organization.