.NET Framework - Active Directory output?

Asked By Russell
31-Jan-10 03:27 PM
Hello,

I am trying to list all the OUs in the domain but when I run the script
below, the OUs under OUs are listed at the end of the list instead of after
the OU it is under.  For instance, I have OUs and sub OUs like so:
--OUab
--OUcd
--OUef
--OUgh
when I run the script the list is:
OUab
OUef
OUcd
OUgh
I would like it to be:
OUab
OUcd
OUef
OUgh

Any ideas how to do this?


$ADsPath = [ADSI]"LDAP://DC=Contoso,DC=Internal"
$Search = New-Object DirectoryServices.DirectorySearcher($ADsPath)
$Search.filter = "(objectClass=OrganizationalUnit)"
$Search.PageSize = 1000
$Search.SearchScope = "SubTree"; $results = $Search.Findall()

Foreach($result in $results)
{
$OU = $result.GetDirectoryEntry()
$OU.Name
}
Active Directory
(1)
DirectoryServices.DirectorySearcher
(1)
Search.SearchScope
(1)
Search.PageSize
(1)
Search.Findall
(1)
WalkDSTree
(1)
Result.GetDirectoryEntry
(1)
Search.filter
(1)
  Chris Dent replied to Russell
01-Feb-10 05:51 AM
I am a bit short of a neater option at the moment. But this will work for
a start. You'll probably want to strip out the Write-Host and indenting
it is doing, those are just so you can see what it is up to.



Function WalkDSTree ( [ADSI]$OU = [ADSI]"", [Int]$i = 0 )
{
$i++

$OU.PsBase.Children |
?{ $_.objectClass -eq "organizationalUnit" } | %{
Write-Host "$(' ' * $i)$($_.Name)"
WalkDSTree $_ $i
}
}
WalkDSTree
  Russell replied to Chris Dent
01-Feb-10 12:47 PM
Thank you.

Russell
Create New Account
help
you everyone who has = tackled getting at this info): PS H: \ # $searcher = 3D New-Object DirectoryServices.DirectorySearcher PS H: \ # $searcher.filter = 3D "(&(samaccountname = 3Dtestuser))" PS H: \ # $results = 3D $searcher.findone() PS H at this info):<BR> <BR> <FONT face = 3DTerminal> PS H: \ # = $searcher = 3D = 20 New-Object DirectoryServices.DirectorySearcher<BR> PS H: \ # = $searcher.filter = 3D = 20 $searcher.findone()<BR> PS H: \ # = 20 $results.properties you everyone who has = tackled getting at this info): PS H: \ # $searcher = 3D New-Object DirectoryServices.DirectorySearcher PS H: \ # $searcher.filter = 3D "(&(samaccountname = 3Dtestuser))" PS H: \ # $results = 3D $searcher.findone() PS H getting at this info):<BR> <BR> <FONT face = 3DTerminal> PS H: \ = $searcher = 3D New-Object DirectoryServices.DirectorySearcher<BR> PS H: \ = $searcher.filter = 3D "(&(samaccountname = 3Dtestuser))"<BR> PS H: \ # = $results = 3D = 20 $searcher
NET Framework Casting a Directory Search result for export to CSV Hello, I'm having a spot of trouble getting a simple directory search to output to a csv file, as each field gets filled with though output to the screen is displayed correctly. Essentially, my script queries an Exchange 5.5 directory (I know!) and retrieves any matching primary email addresses and adds these to an object DirectoryServices.DirectoryEntry "LDAP: / / <server> / cn = Recipients, ou = <site> , o = <org> " $a = @() $Search = new-object System.DirectoryServices.DirectorySearcher($root) $Search.filter = "(&(objectClass = person))" $Search.PageSize = 99 $users = $search.findall() foreach ($user in $users){ if ($user.properties.mail -match "@search.com"){ $list = new-Object
System.DirectoryServices.AuthenticationTypes]::FastBind $root.psbase.path = "LDAP: / / cn = Deleted Objects, " + $root.distinguishedname $search = [System.DirectoryServices.DirectorySearcher]$root $search.Filter = "(&(isDeleted = TRUE)(objectclass = user))" $search.tombstone = $true $search.SearchScope = [System.DirectoryServices.SearchScope]::OneLevel $result = $search.Findall() $result I've had some success with: for ($a = 0; $a -le $result.count; $a root.psbase.path = "LDAP: / / cn = Deleted Objects, " + $root.distinguishedname constructor, but thats just me :) System.DirectoryServices.DirectorySearcher($root, $filter) $search = [System.DirectoryServices.DirectorySearcher]$root $search.Filter = "(&(isDeleted = TRUE)(objectclass = user))" $search.tombstone
NET Framework AD Search.findall() and the pipeline When doing an AD search such as the following from the command at least) $dom = [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain() $root = $dom.forest.rootdomain.getdirectoryentry() $search = [System.DirectoryServices.DirectorySearcher]$root $search.filter = “(&(objectcategory = person)(objectclass = user))” $search.CacheResults = $false $search.PageSize = 10 $search.findall() so why when I do similar but use a pipeline: $search.findall() | format-table path