active directory - Powershell to get user attributes given samaaccount -


i have notepad list of 100 users. use below script users within 1 ou time have users different ou , have search using samaccountname.

clear   $userinfofile = new-item -type file -force "c:\scripts\userinfo.txt"   "login`tgivenname`temail" | out-file $userinfofile -encoding ascii   import-csv "c:\scripts\ou.txt" | foreach-object {    $dn = $_.dn    $objfilter = "(&(objectcategory=user)(objectcategory=person))"    $objsearch = new-object system.directoryservices.directorysearcher    $objsearch.pagesize = 15000    $objsearch.filter = $objfilter    $objsearch.searchroot = "ldap://$dn"    $allobj = $objsearch.findall()  foreach ($obj in $allobj)        { $objitems = $obj.properties               $ssamaccountname = $objitems.samaccountname               $ssamaccountnamegn = $objitems.givenname               $ssamaccountnamesn = $objitems.sn               $ssamaccountnameen = $objitems.mail              "$ssamaccountname`t$ssamaccountnamegn`t$ssamaccountnamesn`t$ssamaccountnameen" | out-file $userinfofile -encoding ascii -append        } # end of foreach    } # end of foreach-object  

i trying use list of samaccountname name , email of users. new powershell above script bit difficult me grasp , on more difficult task.

not sure if understand question correctly, if want filter user list account names this:

$accounts = get-content userlist.txt  ...  $objsearch.findall() | ? {   $accounts -contains $_.properties.samaccountname } | % {   "{0}`t{1}`t{2}" -f ($_.properties.givenname, $_.properties.sn, $_.properties.mail) } 

btw, i'd recommend using activedirectory powershell module if possible. allow retrieve user accounts simple get-aduser, simplifying code. ou can extracted user's distinguished name splitting dn @ first comma. this:

$accounts = get-content userlist.txt get-aduser * -properties * | ? {   $accounts -contains $_.properties.samaccountname } | select @{n="ou";e={($_.distinguishedname -split ",", 2)[1]}}, mail, givenname, sn, homedirectory 

or this:

get-content userlist.txt | get-aduser -properties * |   select @{n="ou";e={($_.distinguishedname -split ",", 2)[1]}}, mail, givenname, sn, homedirectory 

untested, though, since don't have ad @ hand here.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -