Solved

How to get displayname from manager attribute

Posted on 2016-10-24
5
110 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 85

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 85

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 85

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Suggested Courses

626 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