Created
January 11, 2017 08:42
-
-
Save piers7/6e909087cda19187c5da118dfcc94a2c to your computer and use it in GitHub Desktop.
Helpful Active Directory scripts for finding user records (before I lose them again)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ADObject($adspath){ | |
$obj = new-object System.DirectoryServices.DirectoryEntry "LDAP://$adspath"; | |
new-object PSObject -Property:$obj.Properties; | |
} | |
function Find-ADUserByLogin($login){ | |
$ds = new-object System.DirectoryServices.DirectorySearcher | |
$ds.Filter = "(samAccountName=$login)"; | |
$result = $ds.FindOne(); | |
if($result){ | |
$memberof = $results.Properties.memberof; | |
$obj = new-object psobject -Property $result.Properties | |
Add-Member -InputObject:$obj -MemberType:ScriptMethod -Name:GetGroups -Force:$true -Value:{ | |
$memberof | % { Get-ADObject $_ }; | |
}.GetNewClosure(); | |
$obj; | |
} | |
} | |
function Find-ADUserByName($firstName, $lastName){ | |
$ds = new-object System.DirectoryServices.DirectorySearcher | |
$ds.Filter = "(&(objectCategory=user)(givenName=$firstName)(sn=$lastName))"; | |
$result = $ds.FindOne(); | |
if($result){ | |
$memberof = $results.Properties.memberof; | |
$obj = new-object psobject -Property $result.Properties | |
Add-Member -InputObject:$obj -MemberType:ScriptMethod -Name:GetGroups -Force:$true -Value:{ | |
$memberof | % { Get-ADObject $_ }; | |
}.GetNewClosure(); | |
$obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment