Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

SQLTechnet

Vinoth is currently a Senior SQL Server DBA and 10 years of experience as SQL Server DBA. Started my career as a DBA in SQL 6.5/7 has worked in all subsequent SQL Server version. Vinoth has worked in some of the largest SQL server environments in the world in various domains ranging from Finance, Retail, Manufacturing, Consulting, Web etc. Vinoth has Engineering Degree in Computer Science and has certified in MCITP - Database Adminstrator in 2008/2005, MCDBA and ITIL Foundation V3.

Find AD user detail - PowerShell Script

I developed the below script to find the details of a AD user and the AD groups he is associated with, Script 1 is user search by AD user ID and Script 2 is search by AD user name(Last or First Name)

Script 1:- 

#

# Script to Find AD User Details
# Created by - Vinoth N Manoharan
# Version 1.1
# Date - 15/09/2011
Please Enter $usr variable with AD user ID you want to Search

Clear-Host
$usr = "Ad user ID"
$objItem = @()
$strFilter = "(&(objectCategory=User)(name=$usr))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
#$objDomain |Get-Member
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
#$objSearcher | Get-Member
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
#$colProplist = "name"
#foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i);}
$colResults = $objSearcher.FindAll()
#$colResults|Get-Member
foreach ($objResult in $colResults)
{
$objItem = $objResult.GetDirectoryEntry()
#$objItem|Get-Member
"`n"
"Name: " + $objItem.givenName
"Display Name: " + $objItem.displayName
"Account Name: " + $objItem.sAMAccountName
"Department: " + $objItem.department
"TelephoneNumber: " + $objItem.telephoneNumber
"Member Of:"
"----------"
foreach($membergrp in $objItem.memberOf)
{
$objuser1 = New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$membergrp)
"`t"+$objuser1.name
}
#Member Of: `n" +$objItem.memberOf
#$objItem.displayName
}

Script 2:- 

#
# Script to Find AD User Details
# Created by - Vinoth N Manoharan
# Version 1.1
# Date - 15/09/2011
#Please Enter $usr variable with AD user name you want to Search

Clear-Host
$usr = "Vinoth"
$objItem = @()
$strFilter = "(&(objectCategory=User)(|(givenName=*$usr*)(sn=*$usr*)(name=*$usr*)))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
#$objDomain |Get-Member
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
#$objSearcher | Get-Member
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
#$colProplist = "name"
#foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i);}
$colResults = $objSearcher.FindAll()
#$colResults|Get-Member
foreach ($objResult in $colResults)
{
$objItem = $objResult.GetDirectoryEntry()
#$objItem|Get-Member
"`n"
"Name: " + $objItem.givenName
"Surname: " + $objItem.sn
"Display Name: " + $objItem.displayName
"Account Name: " + $objItem.sAMAccountName
"Department: " + $objItem.department
"TelephoneNumber: " + $objItem.telephoneNumber
"Member Of:"
"----------"
foreach($membergrp in $objItem.memberOf)
{
$objuser1 = New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$membergrp)
"`t"+$objuser1.name
}
#$objItem.memberOf
#$objItem.displayName
}

Comments

Leave a comment on the original post [www.sqltechnet.com, opens in a new window]

Loading comments...