简体   繁体   中英

Extracting AD records attributes from a group but need manager's email address as well

I have this script that gets all the members from a security group and then extracts to a file a bunch of their attributes. It works correctly except to pull the manager's email address. The weird thing is though if I run this command to get the manager's email address directly in PowerShell it does return their email address.

Here is the entire script:

Get-ADGroupMember -Identity "ACP Users" -Recursive |
    Get-ADUser -Property employeeNumber, SN, Manager, GivenName, Name, Office,
        Mobile, emailaddress, Department, Title, 
        samaccountname, officephone, homephone |
    select employeeNumber,SN, GivenName,Manager, Office, Mobile, 
        emailaddress,Department, Title, samaccountname, 
        officephone,homephone,enabled | 
    Select-object @{Name='Empno';Expression={$_."employeeNumber"}},
        @{Name='EmployeeName';Expression={$_."GivenName" + ' ' + $_."SN"}},
        @{N='Manager';E={(Get-ADUser $_.Manager).Name}},
        @{N='ManagerSAM';E={(Get-ADUser $_.Manager).samaccountname}},
        @{N='ManagerEmail';E={(Get-ADUser(Get-ADUser $._samaccountname -Properties manager).manager -properties mail).mail}},
        @{Name='EmployeeEmail';Expression={$_."emailAddress"}},
        @{Name='Office';Expression={$_."Office"}},
        @{Name='Title';Expression={$_."Title"}},
        @{Name='Department';Expression={$_."Department"}},
        enabled |
    Export-Csv -Path C:\temp\ACP_Uers.csv -NoTypeInformation

IF I run this part manually in PowerShell it returns an Active Directory user record manually it works just fine:

(Get-ADUser(Get-ADUser chuck.east -Properties manager).manager -Properties mail).mail

File output when ran as the whole script, you can see the email is blank for the manager but it was able to get the manager and manager's sam.

"Empno","EmployeeName","Manager","ManagerSAM","ManagerEmail","EmployeeEmail","Office","Title","Department","enabled"
"8921","Chuck East","Jim Dean","jim.dean",,"Chuck.East@sb.com","East","BSA","IT","True"

Any idea as to why it's not pulling the manager's email?

@{N='ManagerEmail';E={
    (Get-ADUser(
        Get-ADUser $._samaccountname - Properties manager).manager -properties mail).mail
    }
},

You have a typo, $._samaccountname I think. Should be $_.samaccountname

I'm not sure why you departed from the earlier pattern. This should work:

@{N='ManagerEmail';E={
        Get-ADUser $_.manager -properties mail).mail
    }
},

Of course, you could get the manager object once instead of three times... And per Matt's suggestion, build the object instead of using Select-Object, something like this: (untested... On my phone)

Get-ADGroupMember -identity “ACP Users” -Recursive | get-aduser -Property employeeNumber,SN, Manager, GivenName, Name,Office, Mobile, emailaddress,Department, Title, samaccountname,officephone,homephone | Foreach-Object {
    $manager = Get-ADUser $_ -Properties email
    $outputData = [ordered] @{
            Empno=$_."employeeNumber"
            EmployeeName=$_."GivenName" + ' ' + $_."SN"
            Manager=$manager.distinguishedName
            #etc for the other properties you want
    }
    New-object psobject -properties $outputData
} | Export-csv -path C:\temp\ACP_Uers.csv -NoTypeInformation

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