locked
For each loop to find users manager RRS feed

  • Question

  • I'm trying to get a list of users that are managed by a few managers. This for each loop only sees first name in the text file and does not loop to the second line of the text file. Does anyone have any idea why?? 

    $man=get-content"C:\test.txt"


    foreach

    ($manin$mana)

    {

     


    Get-ADUser

    -Filter{ manager-eq$mana} |selectname


    }

    Thursday, January 17, 2019 8:55 PM

All replies

  • I believe you are confusing variables, $Man and $Mana. Also learn to use the code insertion tool in the Wiki Editor to format code.

    $Man = Get-Content "C:\test.txt"
    
    ForEach ($Mana In $Man)
    {
        Get-ADUser -Filter{Manager -eq $Mana} |Select Name
    }
    Edit: This assumes that the values in the file are either distinguished names or sAMAccountNames. And the names must be on separate lines.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)




    Thursday, January 17, 2019 9:01 PM
  • Please do not post colorized code in these forums.  Use the code posting tool provided and it will strip the HTML from your code.  Be sure the code is formatted so it is readable.


    \_(ツ)_/

    Thursday, January 17, 2019 9:01 PM
  • Please read the following carefully to see your errors.

    help about_foreach


    \_(ツ)_/

    Thursday, January 17, 2019 9:02 PM
  • Another way:
    You can loop those few managers instead and look for attribute "directReports" to get desired users.

    .. And if your test.txt contains managers username, your code simply cant work either way.. 

    • Edited by Mekac Thursday, January 17, 2019 9:41 PM
    Thursday, January 17, 2019 9:39 PM
  • I believe you are confusing variables, $Man and $Mana. Also learn to use the code insertion tool in the Wiki Editor to format code.

    $Man = Get-Content "C:\test.txt"
    
    ForEach ($Mana In $Man)
    {
        Get-ADUser -Filter{Manager -eq $Mana} |Select Name
    }
    Edit: This assumes that the values in the file are either distinguished names or sAMAccountNames. And the names must be on separate lines.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)




    I would somehow miss the Column "Manager" in the report - to associate user with his manager.   I know u just corrected his code but for me I would miss something :).

    $managers |% {
        get-aduser $_ -pv mgr -Properties directreports |
            select -ExpandProperty directreports |%{
                get-aduser $_ -properties mail,title |
                    select @{n="manager";e={$mgr.name}},name,mail,title
                 }
        }

    Friday, January 18, 2019 9:04 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, January 22, 2019 6:33 AM