Monday, August 31, 2009

Active Directory Powershell Utils

These four basic fuctions make life so much better when dealing with AD:


$ds = new-object system.directoryservices.directorysearcher

function Find-Group($samName){
$ds.filter = ('(samaccountname={0})' -f $samName)
$ds.FindAll()
}

function Find-User($samName){
$ds.filter = ('(samaccountname={0})' -f $samName)
$ds.FindAll()
}

function Find-Members($groupName){
$group = Find-Group $groupName
$group.Properties.member | Get-Entry
}

function Get-Entry($cn){
begin{
if($cn){
new-object System.directoryservices.DirectoryEntry("LDAP://$cn")
}
}
process{
if($_){
new-object System.directoryservices.DirectoryEntry("LDAP://$_")
}
}
end{
}
}


(ok, three functions. Two are the same, I know)

No comments:

Popular Posts