Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Created December 1, 2016 02:14
Show Gist options
  • Save JustinGrote/8592dae6d6b00fed2e674658ceb1f096 to your computer and use it in GitHub Desktop.
Save JustinGrote/8592dae6d6b00fed2e674658ceb1f096 to your computer and use it in GitHub Desktop.
Get AD Computers using legacy ADSI Interface (Useful for Windows 2003 and areas where AD Web Services is not available for Get-ADComputer)
<#
.EXAMPLE
Get Enabled Computers from a domain in another environment
Get-ADSIEnabledComputer -connectionstring 'LDAP://myserver1.contoso.com/DC=contoso,dc=com' -username "admin" -password "Password@1307"
#>
#TODO: Convert Username/Password to PSCredential field for security
param (
[string]$connectionstring,
[string]$username,
[string]$password
)
$adsi = New-Object System.DirectoryServices.DirectoryEntry($connectionstring,$username,$password)
$searcher = [adsisearcher]($adsi)
$searcher.filter = "(&(objectCategory=computer)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
#Convert the resultset into a PSCustomObject by iterating through the properties
$searcher.findall().properties | foreach {
$entryObject = @{}
foreach ($key in $_.keys) {
#Everything comes back as arrays. If there is only one entry, "strip" the array
if ($_.$key.count -eq 1) {
$entryObject.$key = $_.$key[0]
} else {
$entryObject.$key = $_.$key
}
}
[PSCustomObject]$entryObject
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment