Reflections on [ADSI] and [ADSISEARCHER]
What the heck is [ADSI] and [ADSISEARCHER] ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# What the heck is [ADSISEARCHER] ? [adsisearcher]"" | gm # TypeName: System.DirectoryServices.DirectorySearcher $DirectorySearcherObject = [adsisearcher]""  # This object has an empty filter... $DirectorySearcherObject.Filter  # So let's set a filter with my computer named LA0015 $DirectorySearcherObject.Filter = "name=LA0015"  # The FindOne method will...find something in AD...but what? $DirectorySearcherObject.findone() | gm # TypeName: System.DirectoryServices.SearchResult $SearchResultObject = $DirectorySearcherObject.findone()  # Now you can get the distinguished name of the computer by using the Path method... $computerDN = $SearchResultObject.Path.TrimStart("LDAP://")  # So what is [ADSI] then? I takes the distinguished name as parameter including the LDAP:// prefix [ADSI]”LDAP://$ComputerDN” | gm # TypeName: System.DirectoryServices.DirectoryEntry $DirectoryEntryObject = [ADSI]”LDAP://$ComputerDN”  # What can I do with the DirectoryEntry object? Well...I can move it! $DirectoryEntryObject.MoveTo("LDAP://OU=Desktops,OU=Computers,OU=Company,DC=PTEST,DC=LOCAL") |
All this is useful if you don’t have the PowerShell AD module available, e.g. in WinPE. Enjoy 🙂