Searching the Active Directory with PowerShell

Function get-dn ($SAMName)
{
$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName))"
$user = $searcher.findall()
if ($user.count -gt 1)
{
$count = 0
foreach($i in $user)
{
write-host $count ": " $i.path    
$count = $count + 1
}
$selection = Read-Host "Please select item: "
return $user[$selection].path
}
else
{
return $user[0].path
}
}
$Name = $args[0]
$path = get-dn $Name
"'" + $path + "'"
舉例:
PS C:\store\ps scripts> .\get-dn.ps1 administrator
'LDAP://CN=Administrator,CN=Users,DC=umpadom,DC=com'
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章