Searching Adsi Using Powershell

Searching Adsi Using Powershell

Good morning. I’ve been trying to put together something on and off for days (that should be) simple using PS but I haven’t been able to figure it all out yet. I am trying to search and retrieve information from ADSI using PS. When I use the following in the PS console:

Get-ADObject "CN=OID,CN=Public Key Services,CN=Services,CN=Configuration,DC=domain,DC=com" -Properties *

It returns the object properties that have values assigned to them (which is correct) so that works. But, what I need are children of the AD object, so I try to add SearchScope:

Get-ADObject "CN=OID,CN=Public Key Services,CN=Services,CN=Configuration,DC=domain,DC=com" -SearchScope OneLevel -Properties *

And I receive an error message reporting:

A positional parameter cannot be found that accepts argument "CN=OID,CN=Public Key Services,CN=Services,CN=Configuration,DC=domain,DC=com"

So, I tried a different approach:

[string]$strCrLf = "`r`n"
[string]$strDoubleQuote = '"'
$ChildObjects = @()

$ParentObject = ([ADSI]"LDAP://CN=OID,CN=Public Key Services,CN=Services,CN=Configuration,DC=domain,DC=com")
ForEach ($Child in $ParentObject.psBase.Children)
    {
    $ChildObjects += @("([ADSI]" + $strDoubleQuote + $Child.Path + $strDoubleQuote + ").distinguishedName" + $strCrLf)
    }

$ChildObjects | ForEach-Object {
    Invoke-Expression $_
    }

The above does return the distinguishedName of the children, but how do I retrieve additional properties of the children, such as displayName, Name and whenCreated. If I can get that, then I can work on putting it all into an array that can be exported to a CSV file or something. Do I need to use something like LDAP instead?

2

1 Answer

The formatting you're doing in your first loop isn't really necessary. Try using the following:

$ChildItems = ([ADSI]"LDAP://CN=OID,CN=Public Key Services,CN=Services,CN=Configuration,DC=domain,DC=com")

$ChildItems.psbase.Children | Format-Table Name, DisplayName, whenCreated | Out-File C:\test.csv
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.