Martin Wood
Martin Wood

Reputation: 77

how to provide server/OU/DC details to [adsisearcher]?

$ADResult = ([adsisearcher]"(samaccountname=$sams)").Findone()

I am using this statement to search in the AD the accounts with the samaccountnames as $sams, but the problem is that Im calling this from a different server and not from the one where AD's exist.

So, what I need to know is can I provide it details of the following -

  1. Server
  2. DC
  3. OU

and if yes, HOW?

Upvotes: 1

Views: 10122

Answers (1)

Avshalom
Avshalom

Reputation: 8889

Set variables for your Domain Controller, Domain, Suffix and OU like this:

$DC = "DCServer"
$Domain = "MyDomain"
$Sufix = "Local"
$OU = "MyOU"
$SAMName = "SamAccountName"

Link your Searcher object to that info...

$Root = [adsi] "LDAP://$DC/OU=$OU,DC=$Domain,DC=$Suffix"
$Searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$Searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName))"
$Searcher.FindOne()

If you have Sub-OU's Add OU="OU1",OU="OU2" etc.

Upvotes: 4

Related Questions