# Get75Users3.ps1 # PowerShell script to retrieve the Distinguished Names of 75 objects # in Active Directory from the sAMAccountName values. # # ---------------------------------------------------------------------- # Copyright (c) 2011 Richard L. Mueller # Hilltop Lab web site - http://www.rlmueller.net # Version 1.0 - January 17, 2011 # # This program uses ADO to query Active Directory for 75 objects with # given values of the sAMAccountName attribute. This program makes 75 # separate queries of Active Directory. The program tracks how long # this process takes. The program repeats the 75 queries 10 times at # random intervals between 10 and 15 minutes apart. # # You have a royalty-free right to use, modify, reproduce, and # distribute this script file in any way you find useful, provided that # you agree that the copyright owner above has no warranty, obligations, # or liability for such use. Trap {"Error: $_"; Break;} Function QueryAD($Count, $arrNames) { $dtmInitial = Get-Date $Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() $Root = $Domain.GetDirectoryEntry() $adoConnection = New-Object -comObject "ADODB.Connection" $adoCommand = New-Object -comObject "ADODB.Command" $adoConnection.Open("Provider=ADsDSOObject;") $adoCommand.ActiveConnection = $adoConnection $adoCommand.Properties.Item("Page Size") = 100 $adoCommand.Properties.Item("Timeout") = 30 $adoCommand.Properties.Item("Cache Results") = $False $strBase = $Root.distinguishedName $strAttributes = "distinguishedName" $dtmStart = Get-Date ForEach ($Name In $arrNames) { $strFilter = "(sAMAccountName=$Name)" $strQuery = ";$strFilter;$strAttributes;subtree" $adoCommand.CommandText = $strQuery $adoRecordset = $adoCommand.Execute() Do { $adoRecordset.Fields.Item("distinguishedName") | Select-Object Value > $Null $adoRecordset.MoveNext() } Until ($adoRecordset.EOF) $adoRecordset.Close() } $adoConnection.Close() $dtmEnd = Get-Date Add-Content -Value "-- $Count --" -Path "c:\PowerShell\F75U3.txt" Add-Content -Value $($dtmStart - $dtmInitial) -Path "c:\PowerShell\F75U3.txt" Add-Content -Value $($dtmEnd - $dtmStart) -Path "c:\PowerShell\F75U3.txt" Add-Content -Value $($dtmEnd - $dtmInitial) -Path "c:\PowerShell\F75U3.txt" } # Specify an array of NT names to be translated. $Names = @("r_mueller", "oregon`$", "klmueller", "SBecker", ` "SRBecker", "RKWilliard", "FMadison", "TJefferson", "BFranklin", ` "JSmith", "JAdams", "PocketLunch", "School", "minnesota`$", ` "JMonroe", "PLUser1", "PLUser2", "guest", "rlmueller", ` "FKaplan", "AReed", "fwilliamson", "JJSmith", "First_Last", ` "LFirst", "W.Smith", "Very Long Name 67890", "User 5", ` "VeryLongUserName4321", "RSmith", "WJohnson", "B#Test", "C_Test", ` "JPolk", "FirstMLast", "SallyCrawford", "JohnAdams", "AlGorney", ` "EZastera3", "EZastera2", "EZastera1", "EKent", "JKirk", ` "michelegeiger", "wrugby", "rrogers", "FJackson", "JackJones", ` "jmpolk", "PLTest", "JamesW", "A_Test", "Administrator", ` "Win2kXP", "TemplateUser", "F(M)Last", "GWashington", ` "VeryLongNameForTesti", "vcmgt0j6", "mo'brian", "PocketLunchSvc`$Acct", ` "FMalloy", "fjones", "PL-User3", "Test22", "J Smith", "krbtgt", ` "``~^!@`$%&-_", "Burke William", "Wisconsin`$", "Washington`$", ` "Wyoming`$", "Nebraska`$", "TsInternetUser", "IUSR_WISCONSIN") # Repeat 9 times. $j = 1 While ($j -le 9) { # Query AD. QueryAD $j $Names # Select random seconds from 10 minutes to 15 minutes. # Get-Random requires PowerShell V 2.0. In V 1.0 hard code 750 seconds. $Pause = Get-Random -minimum 600 -maximum 900 Start-Sleep $Pause $j = $j + 1 } # Query AD 10th time. QueryAD $j $Names