PowerShell: Generating AD Accounts Status Report


When I worked as a Windows system administrator, reviewing the Active Directory domain accounts is part of my routine work. We did not have a tool that can automatically generate a account report. So I wrote a PowerShell script to do the work.

The script will generate a report in .csv format, which contains the information: account name, account description, when the account was created, lastest time the password was reset and memberships.

You can do more analysis work if you import the file into Excel.

***************************Start**********************************

cls

$searcher = new-object DirectoryServices.DirectorySearcher([ADSI]””)
$searcher.filter = “(&(objectcategory=user))”
$Searcher.SearchRoot =”LDAP://OU=employees,DC=Test,DC=Com”
$Searcher.CacheResults = $true
$Searcher.SearchScope = “Subtree”
$userlist=$searcher.findall()

$date = $(Get-Date -UFormat “%y-%m-%d-%H:%M”).tostring()
echo “SERVICE_ACCOUNTS_LIST Updated on $date” > service_accounts.csv
echo “Name,Descriptions,Account_Created_Date,Password_Lastset_Date,Member_of” >> service_accounts.csv

foreach ($user in $userlist)
{
$name = $($user.properties.cn).tostring()

if($user.Properties.description -ne $null)
{$notes = $($user.properties.description).tostring()}
else
{$notes = “N/A”}

$whencreated = $($user.properties.whencreated).tostring()

$pwdlastset = [datetime]::fromfiletime(($user.properties.pwdlastset)[0])

if($user.Properties.memberof -ne $null)
{$memberof = $($user.properties.memberof).tostring()}
else
{$memberof = “N/A”}

if($user.Properties.lastlogontimestamp -ne $null)
{$lastlogon = [datetime]::fromfiletime(($user.properties.lastlogontimestamp)[0])}

out-file -InputObject $($name+”,”+$notes+”,”+$whencreated+”,”+$pwdlastset+”,”+$memberof) service_accounts.csv -Append
}

***************************End**********************************

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s