tags:

views:

56

answers:

1

I'm trying to grab out some information from Active Directory using Powershell, but I get some strange behavior. Here's my script:

$toFind = ( 'bobjones', 'samsmith' )

filter Get-AdUser {
    $strFilter = "(&(objectCategory=User)(sAMAccountName=$_))"

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = $strFilter

    $colProplist = ("name", "department")
    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

    ($objSearcher.FindAll() | %{$_.Properties})
}

"paul" | get-aduser # Works
$toFind | get-aduser # Doesn't work?!

The former prints out what I expect, a table of properties; the latter ends up just printing "0 1" repeatedly though I'm not sure why. Why would the single case work but not the array?

+1  A: 

Figured it out, it has nothing to do with PowerShell. When you create the DirectoryEntry:

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

It will return results for your domain only (i.e. if you have an AD forest like "NorthAmerica" and "Europe", it'll only query the one you're in). It just happened that all of the names I was searching for were in another domain.

If you use the constructor to manually specify the Domain, it works pretty well (still haven't figured out how to query all domains yet though...)

Paul Betts