Solved

How to get displayname from manager attribute

Posted on 2016-10-24
5
17 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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 configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

919 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

17 Experts available now in Live!

Get 1:1 Help Now