Forum Discussion

Skipster311-1's avatar
Skipster311-1
Iron Contributor
Dec 07, 2021

Get upn and samaccount name for users mentioned on directreports

Hello all

I am trying to get all disabled user accounts that have users listed on the disabled accounts "directreports" . The below code is showing me the disabled account and the directreports distinguishedname, however i am getting the following error

 

Get-ADUser -Filter * -Properties samaccountname,directreports,enabled | where {$_.directreports -notlike $null -and $_.enabled -eq $False}
             ForEach-Object{
             $mgr = $_
             $_.directreports |
                 ForEach-Object{
                     [array]$d += Get-ADObject $_ |
                                     Select-Object UserPrincipalName, samaccountname
                 }
             $d |
                 ForEach-Object{
                     [PSCustomObject]@{
                         samaccountname = $mgr.samaccountname
                         enabled = $mgr.Enabled
                         distinguishedname = $mgr.distinguishedname
                         directreportupn = $_.userprincipalname
                         directteportsamaccountname = $_.samaccountname
                     }
                 }
             } 

 

 

  •  

    $users = Get-ADUser -Filter * -Properties samaccountname, directreports, enabled | Where-Object { $_.directreports -notlike $null -and $_.enabled -eq $False }
    ForEach ($user in $users) {
        $directreportsupn = (get-aduser -filter * | Where-Object DistinguishedName -Match $user.directreports).UserPrincipalName
        $directreportssamaccountname = (get-aduser -filter * | Where-Object DistinguishedName -Match $user.directreports).SamAccountName
        [PSCustomObject]@{
            samaccountname             = $user.samaccountname
            enabled                    = $user.Enabled
            distinguishedname          = $user.distinguishedname
            directreportupn            = $directreportsupn
            directteportsamaccountname = $directreportssamaccountname
        }
    }

     

    Skipster311-1 Changed the code a little bit, could you verify and let me know if this is what you want?

Resources