Solved

How to get displayname from manager attribute

Posted on 2016-10-24
5
15 Views
Last Modified: 2016-11-13
Basically. i've a text file with bunch of samaccountnames and my task is to get firstname, lastname,email address and manager name out of those samaccountnames. I was able to write below script and 95% job is done but i can't seem to get manager name properly. Below is what i wrote

GC C:\temp\Test.txt| % {Get-Aduser $_ -Pr * | Select GivenName,Surname,displayname,manager} | export-csv C:\temp\Test/csv

but I can't seem to get the display name of the manager. It's coming up similar to Distinguish name attribute.

Any help would be much appreciated

Thanks in advance..
0
Comment
Question by:A D
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 41856732
The "Manager" attribute will contain the manager's DN; you need a calculated property to get the display name on the fly:
Get-Content -Path C:\temp\Test.txt |
	ForEach-Object {
		Get-Aduser -Identity $_ -Properties 'GivenName', 'Surname', 'DisplayName', 'Manager' |
		Select-Object `
			'GivenName',
			'Surname',
			'DisplayName',
			@{Name='Manager'; Expression={If ($_.Manager) {(Get-Aduser -Identity $_.Manager -Properties DisplayName).DisplayName} Else {''}}}
	} | Export-Csv -Path C:\temp\Test.csv -NoTypeInformation

Open in new window

0
 

Author Comment

by:A D
ID: 41856743
Thanks oBdA. It worked fine. You just made my day!!!.

one more thing though, how can i modify the same script to get the samaccountname, emailaddress attribute of managers.

Thanks again!!
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points (awarded by participants)
ID: 41856761
Basically the same way as the DisplayName, but that wouldn't be pretty, because you'd have three AD calls for each user, each retrieving the same Manager object.
This will cache previous AD queries:
Function Get-ManagerProperty([string]$DN, [string]$Property) {
	If (-not $Script:ManagerCache) {$Script:ManagerCache = @{}}
	If (-not $Script:ManagerCache.ContainsKey($DN)) {
		$Script:ManagerCache[$DN] = $(If ([string]::IsNullOrEmpty($DN)) {
			''
		} Else {
			Get-ADUser -Identity $DN -Properties displayName, samAccountName, mail
		}) | Select-Object displayName, samAccountName, mail
	}
	Return $Script:ManagerCache[$DN].$Property
}

Get-Content -Path C:\temp\Test.txt |
	ForEach-Object {
		Get-Aduser -Identity $_ -Properties 'GivenName', 'Surname', 'DisplayName', 'Manager' |
			Select-Object `
				'GivenName',
				'Surname',
				'DisplayName',
				@{Name='ManagerDisplayName'; Expression={Get-ManagerProperty -DN $_.Manager -Property DisplayName}},
				@{Name='ManagerSamAccountName'; Expression={Get-ManagerProperty -DN $_.Manager -Property samAccountName}},
				@{Name='ManagerEmail'; Expression={Get-ManagerProperty -DN $_.Manager -Property mail}}
	} | Export-Csv C:\temp\Test.csv

Open in new window

0
 

Author Comment

by:A D
ID: 41856783
Thanks again. Beautiful Script!!!
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41885208
Question answered.

A D,
please read the following article on how to close your questions properly, once you have your answer:
How do I accept a comment as my solution?
http://support.experts-exchange.com/customer/portal/articles/608621
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now