简体   繁体   中英

Return department of user's manager?

I'm trying to gather some information for a clean-up effort on my company's vendor accounts. One of the bits we want is "what department is their manager in?"

My little script right now will return several other pieces of info, and I can conceptualize how you would need to collect the "manager" attribute for each user, then for THAT user, look at the "department" attribute. But I can't quite see how to proceed.

Here's what I've got. This works but doesn't include the department of the user's manager:

Get-ADUser -Filter * -SearchBase 'ou=Vendors,ou=Users,dc=corporate,dc=coolguys,dc=org' -Properties GivenName, Surname, Company, Title, Manager, EmailAddress |
     select GivenName, Surname, Company, Title, Manager, EmailAddress |
     Out-GridView

Just as a wild hail-Mary I tried returning "manager.department" like it was a value in a variable, but of course PS just scoffed at my ineptitude. Do I need to set a variable that contains all the search results and then use a foreach loop to say "for each user's manager, find that user's department"?

department is a property of a user object. The manager property, however, doesn't contain a user object, but a distinguished name. To get the desired property you need to retrieve the user object corresponding to the manager property and expand its department property.

... | Select-Object GivenName, Surname, Company, Title, Manager, EmailAddress,
  @{n='ManagerDepartment';e={Get-ADUser $_.Manager -Properties department |
     Select-Object -Expand department}} | ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM