Some articles are posted as Images, Please use Computers to go through them for best experience. For phone users, switch to Web Version

AD Script - Fetching User's manager details from Active Directory

 <#Created By - Abhishek Bansal

Read Note

Script Usage :: Fetching User details along with their Manager's name & email ID from AD.

Pre requisites :: Copy all the code into a text file, save it with an extension ".PS1".  A file named Input.txt needs to be created, this file will be containing User Samaccount name. Once saved run it with Admin rights.

Execution & Outputs :: Output_.csv fill will be containing all the results. #>

function getdetails($mgrdn)

{

$mgrdata = Get-ADUser -Properties * -Filter{DistinguishedName -like $mgrdn} | Select Samaccountname,Name,EmailAddress

return $mgrdata

}

$inputuser = Get-Content ./Input.txt

$line = 0 

$linecount = $inputuser.count

$percentagecomplete= 0

$filename = "Output_"+(Get-Date -Format "yyyy_MM_dd")+".csv"

foreach($userid in $inputuser)

{

$line++

$percentagecomplete = ($line / $linecount) * 100

$userid = $userid.trim()

Write-Progress -Activity "Checking Status.." -PercentComplete $percentagecomplete -Status "$line out of $linecount"

[String]$dn = (Get-ADUser -Properties * -Identity $userid).Manager

$managerdetails = getdetails -mgrdn "$dn"

$Error.Clear()

    try

    {

    Get-ADUser -Properties * -Identity $userid | Select Samaccountname,Name,EmailAddress,co,@{n="Manager_Samaccountname";e={$managerdetails.Samaccountname}},@{n="Manger Name";e={$managerdetails.Name}},@{n="Manager Mail";e={$managerdetails.EmailAddress}} `

    | Export-Csv ./$filename -NoTypeInformation -Append

    }

    catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

    {

    $userid | Select @{n="Samaccountname";e={$userid}},@{n="Name";e={$Error.Exception.Message}},EmailAddress,co,@{n="Manager_Samaccountname";e={}},@{n="Manger Name";e={}},@{n="Manager Mail";e={}} `

    | Export-Csv ./$filename -NoTypeInformation -Append

    }

}

No comments:

Post a Comment