我有这个脚本,该脚本从安全组获取所有成员,然后将其属性提取到文件中。除拉出经理的电子邮件地址外,它均能正常工作。奇怪的是,如果我运行此命令直接在PowerShell中获取经理的电子邮件地址,它将返回他们的电子邮件地址。
这是整个脚本:
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
如果我在PowerShell中手动运行此部分,则它会手动返回Active Directory用户记录,因此效果很好:
(Get-ADUser(Get-ADUser chuck.east -Properties manager).manager -Properties mail).mail
作为整个脚本运行时的文件输出,您可以看到经理的电子邮件为空白,但可以获取经理和经理的山姆。
"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"
关于为什么不提取经理的电子邮件的任何想法吗?
答案 0 :(得分:1)
@{N='ManagerEmail';E={
(Get-ADUser(
Get-ADUser $._samaccountname - Properties manager).manager -properties mail).mail
}
},
您有错字,$._samaccountname
,我想。应该是$_.samaccountname
我不确定您为什么偏离了先前的模式。这应该可行:
@{N='ManagerEmail';E={
Get-ADUser $_.manager -properties mail).mail
}
},
当然,您可以一次获得管理器对象,而不是三倍...并且根据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