Skip to content

Instantly share code, notes, and snippets.

@piers7
Created January 11, 2017 08:42
Show Gist options
  • Save piers7/6e909087cda19187c5da118dfcc94a2c to your computer and use it in GitHub Desktop.
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)
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