Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get displayname from manager attribute

Posted on 2016-10-24
5
Medium Priority
?
327 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:P S
  • 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:P S
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 2000 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:P S
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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

963 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