简体   繁体   中英

Get all users with a specific manager

I need to have a list of all users with a specific managers.

this is what I have:


    $manager = Get-user -Filter "UserPrincipalName -eq 'bert@contoso.com'"

    $reports = Get-User -Filter "manager eq '??????'"

    $reports | FL

However I can't get it to work. This script will run against a O365 cloud environment.

I've had a quick look over this an have a method which is working for myself, I tried to style it around how you had set this up originally:

$manager = "Display name"

$reports = get-ADUser -filter * -Properties Manager |
select Name,samaccountname, @{n="ManagerName";e={get-aduser $_.manager | 
select -ExpandProperty name}}, @{n="ManagerEmail";e={get-aduser $_.manager -properties mail | 
select -ExpandProperty mail}} 


$reports | Where-Object {$_.ManagerName -like $manager} | fl

This will grab everyone within your AD, it'll search for 4 main properties:

Name

Samaccountname

ManagerName

ManagerEmail

This will grab a list of users who match manager, you can change:

{$_.ManagerName -like $manager}

to

{$_.ManagerEmail -like $manager}

If you want to search by Email instead.

Doesn't look like you're doing anything with $reports apart from outputting to a list, in which case it's redundant. You ideally want to filter your dataset up-front with whatever criteria you have. In this case you have specific managers, but I'm guessing you can also be more specific about the OU you want to search in.:

Get-ADUser -LDAPFilter (|(Manager=[manager1 distinguishedname])(Manager=[manager2 distinguishedname])(Manager=[manager3 distinguishedname])) -SearchBase "OU=Users,DC=contoso,DC=com" -Properties Manager | Select Name, SAMAccountName, @{n="ManagerName";e={(Get-ADUser $_.Manager).Name}}, @{n="ManagerEmail";e={(Get-ADUser $_.Manager -properties email).email}} | FL

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