Run an LDAP call from powershell..?

rated by 0 users
This post has 4 Replies | 1 Follower

Top 50 Contributor
Posts 69
jwhite Posted: 05-01-2012 10:28 PM

If im in exchange shell, and i want to get a list of all the cas servers in the arrray I would tyep get-clientaccessarray | and grab the members of it.  In turn I would get something like:

server1.company.com,server2.company.com. 

So is there a way to get the list of all the server in the array by doing a LDAP call to AD?  I would like to make the call to ad and see it it runs any quicker.

Any thoughts??

Top 25 Contributor
Posts 287
Top Contributor

$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
$array = @("server1", "server2", "server3")
$root = New-Object System.DirectoryServices.DirectoryEntry(LDAP://dc=contoso,dc=com)
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.SearchRoot = $root
$searcher.PageSize = 1000
$searcher.SearchScope = "Subtree"


foreach($server in $array){

 $searcher.Filter = "(CN=$server)"
 $result = $searcher.Findone()
 $path = [ADSI]$result.path
 $path}

Top 50 Contributor
Posts 69

OK if I run the $root = $dom.GetDirectoryEntry(), part I get:

distinguishedname: {Dc=,Dc=,Dc=}

path: LDAP://...

SO how to I populate :

$root = New-Object System.DirectoryServices.DirectoryEntry(LDAP://dc=contoso,dc=com), with the domain im In?

This is where im getting hung up?  THx.

Top 25 Contributor
Posts 287
Top Contributor

Sorry I think I was sleepy when I wrote that...

Try that instead:

$array = @("server1", "server2", "server3")
$root = New-Object System.DirectoryServices.DirectoryEntry("LDAP://dc=contoso,dc=com")
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.SearchRoot = $root
$searcher.PageSize = 1000
$searcher.SearchScope = "Subtree"

foreach($server in $array){

$searcher.Filter = "(CN=$server)"
$result = $searcher.Findone()
$path = [ADSI]$result.path
$path}

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

The problem is that you need to know the server names to find them in AD - this is self defeating.  The cmdlets search for you.  I don't think you'll find a manual search is any more efficient and depends on you knowing the result before you start!

Page 1 of 1 (5 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.