简体   繁体   English

从组中提取AD记录属性,但也需要经理的电子邮件地址

[英]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. 奇怪的是,如果我运行此命令直接在PowerShell中获取经理的电子邮件地址,它将返回他们的电子邮件地址。

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: 如果我在PowerShell中手动运行此部件,它将手动返回Active Directory用户记录,则效果很好:

(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. 您输入的错字是$._samaccountname Should be $_.samaccountname 应该是$_.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) 当然,您可以一次获得管理器对象,而不是三倍...并且根据Matt的建议,构建对象而不是使用Select-Object,如下所示:(未经测试...在我的手机上)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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