Markdmac sent me this really nice function that converts the sAMAccountName into a Distinguished name. I have a text file with 600 or so usernames that I would like to feed into this function using a foreach loop and send the output to the screen. I am having no luck finding examples on the net on how to do this. I've tried everything I can think of to get this script to work. Any help would be appreciated.
Here is the code:
Here is the code:
Code:
$a = (get-content usernames.txt)
foreach ($name in $a)
[b]{Write-Ouput | Get-DistinguishedName([string]$name)}[/b]
#
function Get-DistinguishedName {
#
Param($username)
#
$ads = New-Object System.DirectoryServices.DirectorySearcher([ADSI]'')
#
$ads.filter = "(&(objectClass=Person)(samAccountName=$username))"
#
$s = $ads.FindOne()
#
return $s.GetDirectoryEntry().DistinguishedName
#
}