Solved

How to get displayname from manager attribute

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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 …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

773 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