Query in AD based on samAccountName and domain


i have text file there ids listed

foo\user1

bar\user2

i need verify whether user exists based on both domain , user name. 

i using code got here

 

$ds = new-object system.directoryservices.directorysearcher $ds.filter = "(&(objectcategory=user)(samaccountname=adam))" $de = $ds.findone()  $de = $de.getdirectoryentry()  if ($de.accountdisabled) {write-host "account disabled" } else { write-host "account enabled"}

however want search on both domain , username , not username.

edit:: ... in above query domain not specified ... searching in domains?

 


msdnstudent knows not much!

try this:

  $domainname = 'contoso.internal' $user = "adam" $domaincontext = new-object system.directoryservices.activedirectory.directorycontext("domain", $domainname) $domain = [system.directoryservices.activedirectory.domain]::getdomain($domaincontext) $root = $domain.getdirectoryentry()  $ds = [adsisearcher]$root  $ds.filter = "(&(objectcategory=user)(samaccountname=$user))" $de = $ds.findone()  $de = $de.getdirectoryentry()  if ($de.accountdisabled) {write-host "account disabled" } else { write-host "account enabled"}   


([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

2008 Windows Deployment Server Properties Error

Can no longer user MS Update - Files required to use Microsoft Update are no longer registered

How do a find data in one file, search for it in another file and if not found, write a custom message to another file